Implementation and consideration of the String class
For strings, it is sometimes difficult for us to compare and directly copy or define them into integer or other variables. This is a problem in C language, in C ++, we provide a type of special solution for this string operation, called the string class. In fact, I also want to implement it and learn about it for a while, the following is my simple implementation of the string class. My string is called the String class, which must be case sensitive !!!!!!!!!
/*/************************************* * ********************************** Copyright (c) 2015, WK Studio ** Filename: String. h ** Compiler: GCC vs2013 ** Author: WK ** Time: 2015 14 6 ************************************* * *********************************/# include
# Include
Using namespace std; class String {public: String (char * d): data (d) {}// constructor String (const char * str = NULL );~ String (); String (const String & s); String & operator = (const String & s); friend String operator + (const String & str1, const String & str2 ); string & operator ++ = (const String & str2); friend String & operator + = (String & str1, String & str2); friend bool operator! = (String & str1, String & str2); friend bool operator = (String & str1, String & str2); friend bool operator> (String & str1, String & str2 ); friend bool operator <(String & str1, String & str2);/* the reload Function Form for "<" and ">" is as follows: istream & operator> (istream &, custom Class &); ostream & operator <(ostream &, custom Class &); that is, the first parameter and function type of the function with the overload operator ">" must be istream & type, and the second parameter is the class to be input. The first parameter and function type of the function with the "<" overload must be ostream & type, and the second parameter is the class for output operations. Therefore, you can only use functions with ">" and "<" as friend functions or common functions, rather than defining them as member functions. */Friend istream & operator> (istream & is, String & str); // return reference can be used for continuous input, the returned value does not exist in the stack, so you can return the reference friend ostream & operator <(ostream & out, String & str); private: char * data ;};
/*/************************************* * ********************************** Copyright (c) 2015, WK Studio ** Filename: main. cpp ** Compiler: GCC vs2013 ** Author: WK ** Time: 2015 14 6 ************************************* * ********************************/void main () {String s1 = "Str"; // call to construct String s2 ("ing"); // call to construct String s3 = s1; // call to copy String s4 ("String"); // call to construct s4 = s2; // call to assign a value to String s5; // call to construct s5 = s2 ;/ /Call String s6 ("String"); // call to construct s6 = s1 + s2; // call + operator overload function returns a temporary unknown object. Call the value assignment function to assign a value to s6 through a temporary unknown object. Then, the temporary unknown object is released as String s7; s7 = s1 + s2; // same as String s8 = s1 + "123"; // call the constructor and use "123" to generate a temporary object and add it to s1. + the operator reloads String s9 = s1 + s2; // + the temporary unknown object returned by the operator overload is directly used to copy and construct the s8 object String s10; s10 + = s1; // If the + = Operator is overloaded, both friends and member functions call the youyuan function // s9.operator + = (s1 ); // call the member function // operator + = (s9, s1); // you can call the member function directly, because the member function is a class that does not need to be driven by objects // String s10 + = s2; // The above cannot be because + = The previous s10 object has not been constructed yet. // It is similar to int a = 10; int B + = a; int c + = 1; String ss ("1 "); string ss1 ("1"); cout <(s1! = S2) <"\ n"; cout <(ss! = Ss1) <"\ n"; cout <(ss = ss1) <"\ n"; cout <(s1 = s2) <"\ n"; cout <(s1> s2) <"\ n"; cout <(s1 <s2) <"\ n"; String s11; cin> s11; cout <s11; getchar ();}
/*/************************************* * ********************************** Copyright (c) 2015, WK Studio ** Filename: String. cpp ** Compiler: GCC vs2013 ** Author: WK ** Time: 2015 14 6 ************************************* * ********************************/# include "String. h "// constructor String: String (const char * str) {cout <" Create String! "<This <" \ n "; if (str = NULL) {data = new char [1]; // there must be at least one '\ 0' data [0] =' \ 0';} else {data = new char [strlen (str) + 1]; // (char *) malloc (strlen (str) + 1); strcpy (data, str );}} /* // constructor 1 String (const char * str = NULL) {if (str = NULL) {str = NULL; // when the string is NULL} else {data = new char [strlen (str) + 1]; // (char *) malloc (strlen (str) + 1); strcpy (data, str) ;}// constructor 2 String (const char * str = "") // cannot meet {data when the String is NULL = New char [strlen (str) + 1]; // (char *) malloc (strlen (str) + 1); strcpy (data, str );} * // Copy to construct a String: String (const String & s) {cout <"Copy String! "<This <" \ n "; data = new char [strlen (s. data) + 1]; strcpy (data, s. data) ;}//= operator reload String & String: operator = (const String & s) // return reference can be connected to {cout <"Assignment String! "<This <" \ n "; if (this! = & S) // (1) Check the self-assigned value {free (this-> data); // (2) release the original memory resource this-> data = NULL; this-> data = new char [strlen (s. data) + 1]; // (3) allocate new memory resources and copy the content strcpy (this-> data, s. data);} return * this; // (4) returns the reference of this object}/* String & operator = (const String & s) {if (this = & s) // (1) Check the self-assigned value {return * this;} free (this-> data); // (2) release the original memory resource data = NULL; data = new char [strlen (s) + 1]; // (3) allocate new memory resources and copy the content strcpy (data, s. data); return * this; // (4) returns the reference of this object Use} * // operator + overload as the member function String operator + (const String & str1, const String & str2) // reference cannot be returned, this is because a temporary object is created in the function stack. For more information, see my article {String str; // call the constructor delete [] str. data; str. data = NULL; str. data = new char [strlen (str1.data) + strlen (str2.data) + 1]; strcpy (str. data, str1.data); strcat (str. data, str2.data); cout <"+ operator String! "<" \ N "; return str; // The copy constructor is called to construct a temporary unknown object by copying the str object, release the temporary object in the stack.} // + = operator is reloaded into the member function String & String: operator + = (const String & str2) {if (this! = & Str2) {* this = * this + str2;} cout <"+ = Operator member function overload! \ N "; return * this;} // + = operator is overloaded as a friend function String & operator + = (String & str1, String & str2) // you can return a reference, because str1 exceeds this function stack, {if (& str1! = & Str2) // when yes! = Can be used after the operator is overloaded (str1! = Str2) Judge {str1 = str1 + str2;} cout <"+ = Operator member function overload! \ N "; return str1 ;}//! = Overload function bool operator! = (String & str1, String & str2) {if (! Strcmp (str1.data, str2.data) {cout <"These String is equal! \ N "; return false;} cout <" These String is not equal! \ N "; return true;} // = operator heavy load bool operator = (String & str1, String & str2) {if (! Strcmp (str1.data, str2.data) {cout <"These String is equal! \ N "; return true;} cout <" These String is not equal! \ N "; return false;} // the heavy-duty stream extraction operator"> "istream & operator> (istream & in, String & str) // is a reference of cin, cin is an istream-like object {cout <"Please input string :( end of 'ctrl + Z')"; char c [101] = {0 }; // The buffer is 100byte char ch = 0; int I (0); // equivalent to I = 0; while (in> ch) {c [I] = ch; I ++;} delete [] str. data; str. data = new char [I + 1]; strcpy (str. data, c); return in;} // The operator "<" ostream & operator <(ostream & out, String & str) "is a cou T references. cout is an ostream-like object {out <str. data; return out;} bool operator> (String & str1, String & str2) {if (strcmp (str1.data, str2.data) = 1) {cout <"First String is big! "; Return true;} cout <" Second String is big! "; Return false;} bool operator <(String & str1, String & str2) {if (strcmp (str1.data, str2.data) = 1) {cout <"First String is big! \ N "; return false;} cout <" Second String is big! \ N "; return true;} // destructor String ::~ String () {cout <"Free String! "<This <" \ n ";}