C ++ string class implementation

Source: Internet
Author: User

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 ;}

 

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.