[C + +] Operator overloading for string string classes

Source: Internet
Author: User
Tags strcmp

In C + + there is a newly defined type string that can be used without the hassle of manipulating strings, and some advanced operator overloads make her easier to use

The following is the definition of the string class and the definition of the member function:

#ifndef operator_operator_h#define operator_operator_h#include<iostream> #include <string.h>using    namespace Std;class string{friend ostream& operator<< (ostream &out, const String &s);        Friend istream& operator>> (IStream &in, String &s);p ublic:string (const char *STR = NULL) {            if (str = = NULL) {m_data = new char[1];        M_data[0] = ' + ';            } else {m_data = new Char[strlen (str) +1];        strcpy (M_DATA,STR);        }} String (const string &s) {m_data = new Char[strlen (s.m_data) +1];    strcpy (M_data,s.m_data);            } string& operator= (const String &s) {if (This! = &s) {free (m_data);            m_data = new Char[strlen (s.m_data) +1];        strcpy (M_data,s.m_data);    } return *this;    } ~string () {delete []m_data; } friend ostream& operator<< (Ostream &Out, const String &s) {out<<s.m_data;    return out;        } friend istream& operator>> (IStream &in, String &s) {in>>s.m_data;    return in; } string operator+ (const string &s); s = s1 + s2 string operator+= (const string &s);    S1 + = S2 char& operator[] (int index);    BOOL operator== (String &s);    BOOL Operator!= (String &s);   BOOL Operator> (String &s);    S1 > S2 bool operator<= (String &s);    BOOL operator< (String &s); BOOL Operator>= (String &s);p Rivate:char *m_data;};    String string::operator+= (const string &s)//s1 + = s2{m_data = new Char[sizeof (m_data) +1];    strcat (M_data,s.m_data); return *this;}    String string::operator+ (const string &s)//s = S1 + s2{strcat (m_data, s.m_data); return *this;}    BOOL string::operator== (String &s) {if (strcmp (m_data, S.m_data) ==0) return true; return false;} BOOL String::operator!= (String &s) {if (strcmp (m_data,s.m_data)!=0) return true; return false;}    BOOL String::operator> (String &s)//s1 > s2{if (strcmp (m_data,s.m_data) >0) return true; return false;}    BOOL String::operator<= (String &s) {if (strcmp (m_data,s.m_data) >=0) return true; return false;}    BOOL string::operator< (String &s) {if (strcmp (m_data,s.m_data) <0) return true; return false;}    BOOL String::operator>= (String &s) {if (strcmp (m_data,s.m_data) <=0) return true; return false;}    char& string::operator[] (int index) {//char*p= m_data;//return * (P+index); return M_data[index];} #endif


The following is the test program:

#include "operator.h" int main () {    String s ("s");    String S1 ("s");    String S2;    String S3;    s2 = s+s1;    S+=S1;    cout<< "s+=s1=" <<s<<endl;    cout<< "s[2] =" <<s[0]<<endl;//    if (s = = S1)//        cout<<1<<endl;//    if (s! = S1 )//        cout<<0<<endl;//    if (S > S1)//        cout<< "S>S1" <<endl;//    if (s >= s1)//        cout<< "S>=S1" <<endl;//    if (S < S1)//        cout<< "S<S1" << endl;//    if (s <=s1)//        cout<< "s<=s1" <<endl;    cin>>s3;    cout<< "s3=" <<s3<<endl;//please test    return 0 separately;}

Enter S3 and don't know why it's capitalized.


[C + +] Operator overloading for string string classes

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.