Algorithm and data structure basics 2: Implementation of the String class
Speak nonsense, reject the prelude, and go straight to the climax, and read the code:
// String. h
# Include
# Include
# Pragma warning (disable: 4996) using namespace std; class String {public: //************************************** * *********************************** // class four functions: constructor, copy constructor, overload assignment operator, destructor //************************ **************************************** * ********* String (const char * str = NULL ); string (const String & str); String & operator = (const String & str );~ String (); //************************************** *********************************** // reload/ /*************************************** * ********************************* String operator + (const string & str) const; String & operator + = (const String & str); bool operator = (const String & str); char & operator [] (unsigned int index ); friend ostream & operator <(ostream & OS, String & str); friend istream & operator> (Istream & is, String & str); unsigned short size (); const char * c_str () const; private: char * m_string ;}; //************************************** * *********************************** // class four functions: constructor, copy constructor, overload assignment operator, destructor //************************ **************************************** * ********* String:: String (const char * str) {if (! Str) {m_string = NULL;} else {m_string = new char [strlen (str) + 1]; strcpy (m_string, str) ;}} String :: string (const String & str) {if (! Str. m_string) {m_string = NULL;} else {m_string = new char [strlen (str. m_string) + 1]; strcpy (m_string, str. m_string) ;}} String & String: operator = (const String & str) {if (this! = & Str) {delete [] m_string; if (! Str. m_string) {m_string = NULL;} else {m_string = new char [strlen (str. m_string) + 1]; strcpy (m_string, str. m_string) ;}} return * this;} String ::~ String () {delete [] m_string; m_string = NULL ;} //************************************** *********************************** // reload/ /*************************************** * ********************************* String:: operator + (const String & str) const {String newString; if (! Str. m_string) {newString = * this;} else if (! M_string) {newString = str;} else {newString. m_string = new char [strlen (m_string) + strlen (str. m_string) + 1]; strcpy (newString. m_string, m_string); strcat (newString. m_string, str. m_string);} return newString;} String & String: operator + = (const String & str) {String newString; if (! Str. m_string) {newString = * this;} else if (! M_string) {newString = str;} else {newString. m_string = new char [strlen (m_string) + 1]; strcpy (newString. m_string, m_string); strcat (newString. m_string, str. m_string);} strcpy (m_string, newString. m_string); return * this;} bool String: operator = (const String & str) {if (strlen (m_string )! = Strlen (str. m_string) {return false;} return strcmp (m_string, str. m_string )? False: true;} char & String: operator [] (unsigned int index) {if (index> = 0 & index <= strlen (m_string )) {return m_string [index] ;}else {cout <"invalid index:" <index <endl; exit (0 );}} ostream & operator <(ostream & OS, String & str) {OS <str. m_string; return OS;} istream & operator> (istream & is, String & str) {char tmp [1024]; is> tmp; str. m_string = new char [strlen (tmp) + 1]; strcpy (str. m_string, tmp); return is;} unsigned short String: size () {return strlen (m_string);} const char * String: c_str () const {return m_string ;}
// Main. cpp
#include "String.h"#include
int main(){String s;cin >> s;cout << s << ":" << s.size() << endl;char a[] = "Hello", b[] = "World";String s1(a), s2(b);cout << s1 << " + " << s2 << " = " << s1 + s2 << endl;String s3 = s1 + s2;if (s1 == s3){cout << "First: s1 == s3" << endl;}s1 += s2;if (s1 == s3){cout << "Second: s1 == s3" << endl;}system("pause");return 0;}
Output result:
Appendix:
Strcpy reports an error in vs2013. For solutions, refer:
Http://blog.csdn.net/u010273652/article/details/21320431