What are the default functions of the string class?
Default constructor
Destructor
Copy constructor
Value assignment Constructor
The specific implementation is as follows:
# Include <iostream> using namespace STD; Class mystring {friend ostream & operator <(ostream & out, const mystring & S); public: mystring (const char * STR = NULL); mystring (const mystring & RHs );~ Mystring (); mystring & operator = (const mystring & other); Private: char * m_data ;}; // default constructor mystring: mystring (const char * Str) {cout <"default constructor call. "<Endl; If (null = Str) {m_data = NULL;} else {m_data = new char [strlen (STR) + 1]; strcpy (m_data, str) ;}}// copy the constructor mystring: mystring (const mystring & RHs) {cout <"" <Endl; If (RHS. m_data = NULL) {This-> m_data = NULL;} else {This-> m_data = new char [strlen (RHS. m_data) + 1]; strcpy (this-> m_data, RHS. m_data) ;}// destructor mystring ::~ Mystring () {cout <"destructor call !! "<Endl; If (this-> m_data! = NULL) {Delete [] This-> m_data; this-> m_data = NULL ;}// the value assignment operator reloads mystring & mystring: Operator = (const mystring & other) {cout <"the value assignment operator is called. "<Endl; If (this! = & Other) {Delete [] This-> m_data; If (! Other. m_data) {This-> m_data = NULL;} else {This-> m_data = new char [strlen (Other. m_data) + 1]; strcpy (this-> m_data, other. m_data) ;}} else {return * This ;}// youyuanfunction ---- output operator ostream & operator <(ostream & OS, const mystring & S) {OS <S. m_data; return OS;} void main () {mystring M ("hello"); // call the default constructor mystring B = m; // call the value assignment operator cout <B <Endl ;}