#include #include using namespace std;class MyString {public: MyString(); MyString(const char* cstr); MyString(const MyString &other); MyString& operator=(const MyString &other); size_t length() { return size; } friend istream& operator>>(istream &in, const MyString &str); friend ostream& operator<<(ostream &out, const MyString &str); ~MyString();protected: char *data; size_t size;};MyString::MyString() { size = 0; data = new char[1]; *data = '\0';}MyString::MyString(const char *cstr) { if (cstr != NULL) { size = strlen(cstr); data = new char[size + 1]; strcpy(data, cstr); } else { size = 0; data = new char[size + 1]; *data = '\0'; }}MyString::MyString(const MyString &other) { if (!other.data) { data = new char[other.size + 1]; *data = '\0'; size = 0; } else { size = other.size; data = new char[1]; strcpy(data, other.data); }}MyString& MyString::operator =(const MyString &other) { /*if (this != &other) { size = strlen(other.data); char *temp = new char[size + 1]; strcpy(temp, other.data); delete []data; data = temp; } return *this;*/ if (this != &other) { MyString strTmp(other); char *pTmp = strTmp.data; strTmp.data = data; data = pTmp; }
return *this;}MyString::~MyString() { delete []data;}int main(){ char *s = NULL; MyString str(s); return 0;}