題目來自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; }
運行結果: