The string class uses some class operations such as assignment constructor, copy constructor, constructor, default constructor, destructor, overload operator, etc.
class String { public: String() { data = new char[1]; //Why use new char[1] here? Although it is a character, this is to keep symmetric with the destructor, because the char[] is used in other constructors data[0]='\0'; length = 0; } String(const char* str) { length = strlen(str); data = new char[length+1]; strcpy(data,str); data[length]='\0'; } String(const char* str,int n) { length = n; data = new char[n+1]; strncpy(data,str,n); data[length] ='\0'; } String(const String& src) //Copy constructor, that is, copy constructor { length = ; data = new char[length+1]; strcpy(data,); } String& operator=(const String& src) //Assignment constructor { if(this == &src) return *this; delete [] data; data = new char[+1]; strcpy(data,); return *this; } String& operator=(const char* src) //Another assignment constructor { delete [] data; length = strlen(src); data = new char[length+1]; strcpy(data,src); return *this; } char& operator[](int n) { return data[n]; } const char& operator[](int n) const //For const member functions, it is best to return a reference to const, so as to avoid the return value being modified { return data[n]; } friend ostream& operator<<(ostream& os,const String& st); ~String() { delete [] data; } private: char* data; int length; }; ostream& operator<<(ostream& os,const String& st) { os<<; return os; }
The above simple implementation case of a string class is all the content I share with you. I hope you can give you a reference and I hope you can support me more.