Self-implemented string class

Source: Internet
Author: User

Implement a string class in C ++ with basic functionality like comparison,
Concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class ).
Please do not use MFC, STL and other libraries in your implementation.
My implementation scheme is as follows. This question really gives a better test of the main features of C ++.

1 Reference and pointer.

Class 2: constructor, destructor, and copy constructor.

3. The operator is overloaded.

4 friends. Private member of the callback class of a non-member function outside the class.

1 mystring. h 

# Ifndef string_h_included <br/> # define string_h_included <br/> # include <iostream> <br/> using namespace STD; <br/> class string <br/>{< br/> PRIVATE: <br/> char * chars; <br/> public: <br/> // constructor <br/> string (); <br/> string (const char *); // use a C string for initialization <br/> string (const string &); // use a string for initialization. Copy the constructor <br/> // destructor <br/> ~ String (); <br/> char & operator [] (int I) const; <br/> // string value assignment <br/> // operator symmetry is not considered, so use the member function. In addition, the by adress return method is used because you are modifying yourself. Returns string & <br/> string & operator = (const string &); <br/> string & operator + = (const string &); <br/> // concatenate strings <br/> // use a non-member function of youyuan in consideration of operator symmetry. And return by value is used. Returns string <br/> friend string operator + (const string &, const string &); <br/> // string comparison. Considering operator symmetry, we use a non-member function of youyuan. <Br/> friend bool operator = (const string &, const string &); <br/> friend bool Operator! = (Const string &, const string &); <br/> friend bool operator> (const string &, const string &); <br/> friend bool operator >=( const string &, const string &); <br/> friend bool operator <(const string &, const string &); <br/> friend bool operator <= (const string &, const string &); <br/> // The output must be a non-member function of youyuan, because the first parameter is not this. The second parameter is const <br/> friend ostream & operator <(ostream &, const string &); <br/> // input. It must be a non-member function of youyuan, because the first parameter is not this. The second parameter is not const <br/> friend istream & operator> (istream &, string &); <br/> // others <br/> int length () const; // return the length of the current string <br/> bool empty () const; // whether the current string is null <br/> }; <br/> # endif // string_h_included <br/> 

See http://blog.csdn.net/HEYUTAO007/archive/2010/09/04/5863364.aspx for how to properly design Operator Overloading and youyuan

2 mystring. cpp 

# Include "mystring. H "<br/> # include <cstring> <br/> # include <cassert> <br/> // constructor <br/> string: string () <br/>{< br/> chars = new char [1]; <br/> chars [0] = '/0 '; <br/>}< br/> string: string (const char * s) <br/>{< br/> If (S = NULL) <br/>{< br/> chars = new char [1]; <br/> chars [0] = '/0 '; <br/>}< br/> else <br/> {<br/> chars = new char [strlen (s) + 1]; <br/> strcpy (chars, s); <br/>}< br/> string: S Tring (const string & S) <br/>{< br/> chars = new char [strlen (S. chars) + 1]; <br/> strcpy (chars, S. chars); <br/>}< br/> // analysis function <br/> string ::~ String () <br/>{< br/> Delete [] chars; <br/> chars = NULL; <br/>}< br/> char & string :: OPERATOR [] (int I) const <br/>{< br/> assert (I <= strlen (chars); <br/> return chars [I]; <br/>}< br/> // string value assignment <br/> string & string: Operator = (const string & S) <br/>{< br/> If (this = & S) <br/> return * This; <br/> Delete [] chars; <br/> chars = new char [strlen (S. chars) + 1]; <br/> strcpy (chars, S. chars); <br/> return * This; <BR/>}< br/> string & string: Operator + = (const string & S) <br/>{< br/> int Len = strlen (chars ); <br/> char * TMP = new char [Len + strlen (S. chars) + 1]; <br/> strcpy (TMP, chars); <br/> strcpy (TMP + Len, S. chars); <br/> Delete [] chars; <br/> chars = TMP; <br/> return * this; <br/>}< br/> // concatenate strings. Youyuan implementation, no string ::. <Br/> string operator + (const string & S1, const string & S2) <br/>{< br/> int Len = strlen (s1.chars ); <br/> char * chars = new char [Len + strlen (s2.chars) + 1]; <br/> strcpy (chars, s1.chars ); <br/> strcpy (chars + Len, s2.chars); <br/> string TMP; <br/> Delete [] TMP. chars; <br/> TMP. chars = chars; <br/> return TMP; <br/>}< br/> // string comparison <br/> bool operator = (const string & S1, const string & S2) <br/>{< br /> Return! Strcmp (s1.chars, s2.chars); <br/>}< br/> bool Operator! = (Const string & S1, const string & S2) <br/>{< br/> return strcmp (s1.chars, s2.chars ); <br/>}< br/> bool operator <(const string & S1, const string & S2) <br/>{< br/> return strcmp (s1.chars, s2.chars) <0; <br/>}< br/> bool operator <= (const string & S1, const string & S2) <br/>{< br/> return strcmp (s1.chars, s2.chars) <= 0; <br/>}< br/> bool operator> (const string & S1, const string & S2) <br/>{< br/> r Eturn strcmp (s1.chars, s2.chars)> 0; <br/>}< br/> bool operator >=( const string & S1, const string & S2) <br/>{< br/> return strcmp (s1.chars, s2.chars)> = 0; <br/>}< br/> // output <br/> ostream & operator <(ostream & cout, const string & S) <br/>{< br/> cout <S. chars; <br/> return cout; <br/>}< br/> // enter <br/> istream & operator> (istream & Cin, string & S) <br/>{< br/> char STR [100000]; <br/> CIN> STR; <br/> Delete [] S. chars; <br/> S. chars = new char [strlen (STR) + 1]; <br/> strcpy (S. chars, STR); <br/> return CIN; <br/>}< br/> // other <br/> int string: length () const // return the length of the current string <br/>{< br/> return strlen (chars); <br/>}< br/> bool string: Empty () const // whether the current string is null <br/>{< br/> return! Strlen (chars); <br/>}< br/> int main () <br/>{< br/> string a ("first "); <br/> string B (a); <br/> string c = A; <br/> cout <A <Endl; <br/> cout <B <Endl; <br/> cout <C <Endl; <br/> cout <(A = B) <Endl; <br/> cout <A [2] <Endl; <br/> C + = A; <br/> cout <C <Endl; <br/> string D; <br/> CIN> D; <br/> cout <D <Endl; <br/> B = a + D; <br/> cout <B <Endl; <br/> cout <. length () <Endl; <br/> cout <. empty () <Endl; <br/> return 0; <br/>}< br/>

/* Running result:

First
First
First
1
R
Firstfirst
Second
Second
Firstsecond
5
0
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.