Default string Constructor
/// # Include <iostream> // using namespace std; // # pragma warning (disable: 4996) // class String // {// public: /// * String () //: _ str (new char [strlen ("") + 1]) // {// * _ str = '\ 0 '; ///} // String (const char * s) //: _ str (new char [strlen (s) + 1]) // {// strcpy (_ str, s); //} * // String (const char * s = "") //: _ str (new char [strlen (s) + 1]) // {// strcpy (_ str, s); //} // String (const String & s) //: _ str (new char [(strlen (s. _ str) + 1]) // {// strcpy (_ str, s. _ str); //} // String & operator = (const String & s) // {// if (this! = & S) // {// delete [] _ str; // The original space must be released when the value is assigned again, otherwise, memory leakage may occur. // _ str = new char [(strlen (_ str) + 1]; // strcpy (_ str, s. _ str); //} // return * this ;//}////~ String () // {// delete [] _ str; //} // void Display () // {// cout <_ str <endl; ///} // private: // char * _ str; //}; // void Test1 () // {///* String s1; // s1.Display (); /// String s2 (s1); // s2.Display (); * /// String s1 ("abcd"); // s1.Display (); // String s2; // s2.Display (); // s2 = s1; // s2.Display (); //} // int main () // {// Test1 (); // system ("pause"); // return 0 ;//}
Pay attention to memory management, so it is easy to leak memory!