C++ | 基于char*设计一个字符串类MyString

题目来自C++语言程序设计(第四版) 作者郑莉 习题6-24

下面是代码基于char*的实现:

/*
 * @Author: Hellcat
 * @Date: 2020-03-24 11:44:47
 * This file is MyString.h
 */
// class with pointer members必须有copy ctor(拷贝构造)和copy op =(拷贝复制)
#ifndef __MyString__
#define __MyString__

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class MyString {
public:
	MyString(const char* cstr = nullptr);
	MyString(const MyString& str);
	MyString& operator = (const MyString& str);
	MyString& operator += (const MyString& str);
	MyString operator + (const MyString& str);
	int length();
	~MyString();
	char* c_str() const { return m_data; }
private:
	char* m_data;
	friend ostream& operator << (ostream&os, const MyString& str);
	friend istream& operator >> (istream&is, MyString& str);
};

  类的实现文件:

inline MyString::MyString(const char* cstr /*= nullptr*/) { //  default argument只能给一次
	if(cstr != nullptr) {
		m_data = new char[strlen(cstr) + 1];     // + 1 分配'\0'
		strcpy(m_data, cstr);
	}
	else { // 未指定初值
		m_data = new char[1];
		*m_data = '\0';
	}
}

inline MyString::MyString(const MyString& str) { // 深拷贝
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
}

inline MyString& MyString::operator = (const MyString& str) {
	if(this == &str) // 检测自我赋值
		return *this;

	delete [] m_data;
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
	return *this;
}

// 返回局部对象 不能return reference
inline MyString MyString::operator + (const MyString& str) {
	MyString res;
	res.m_data = new char[strlen(this->m_data) + strlen(str.m_data) + 1];
	strcpy(res.m_data, this->m_data);
	strcat(res.m_data, str.m_data);
	return res;
}

inline MyString& MyString::operator += (const MyString& str) {
	if(str.m_data == nullptr || *str.m_data == '\0')
		return *this;
	if(this == &str) {    // 检测自我 +=
		MyString temp(*this);
		return *this += temp;
	}
	int len = strlen(m_data) + strlen(str.m_data) + 1;
	char* ptr = m_data;
	m_data = new char[len];
	strcpy(m_data, ptr);
	strcat(m_data, str.m_data);
	return *this;
	// 或者也可以用前面写的 + 和 = 重载
	// *this = *this + str;
	// return *this;
}

inline bool operator <= (const MyString& str1, const MyString& str2) {
    return strcmp(str1.c_str(), str2.c_str()) <= 0;
}

// array new with array delete
inline MyString::~MyString() {
	delete[] m_data;
}

ostream& operator << (ostream&os, const MyString& str) {
    return os<<str.c_str();
}

istream& operator >> (istream&is, MyString& str) {
    char temp[1010];
    if(is>>temp) {
        delete [] str.m_data;
        str.m_data = new char[strlen(temp) + 1];
		strcpy(str.m_data, temp);
    }
	return is;
}

inline int MyString::length() {
    return strlen(this->m_data);
}

  测试代码:

// test135.cpp
#include "MyString.h"

inline void test(const char* title, bool value) {
    cout<<title<<" returns "<<(value ? "true" : "false")<<endl;
}

int main() {
    MyString s1 = "DEF";
    cout<<"s1 is "<<s1<<endl;

    MyString s2;
    cout<<"Please enter s2: ";
    cin>>s2;
    cout<<"length of s2: "<<s2.length()<<endl;
    // 测试比较运算符
    test("s1<=\"ABC\"", s1 <= "ABC");
    test("\"DEF\"<=s1", "DEF" <= s1);
    // 测试连接运算符
    s2 += s1;
    cout<<"s2 = s2 + s1: "<<s2<<endl;
    cout<<"length of s2: "<<s2.length()<<endl;
    return 0;
}

  运行结果:

C++ | 基于char*设计一个字符串类MyString

 

上一篇:造*:新建一个属于自己的String类


下一篇:c++ MyString类实现