String class operator overloading, implement it yourself

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>
#include<string.h>
using namespace std;
class String
{
        private:
                char *pstr_;
        public:
                String();
                String(const char *);
                String(const String&);
                ~String();
                String & operator = (const String &);
                String & operator = (const char *);

                String & operator += (const String &);
                String & operator += (const char *);

                char & operator [] (std::size_t index);
                const char & operator [](std::size_t index)const;

                std::size_t size() const;
                const char* c_str() const;

                friend bool operator == (const String &,const String &);
                friend bool operator != (const String &,const String &);

                friend bool operator < (const String &,const String &);
                friend bool operator > (const String &,const String &);
                friend bool operator <= (const String &,const String &);
                friend bool operator >= (const String &,const String &);

                friend std::ostream & operator << (std::ostream & os,const String &s);
                friend std::istream & operator >> (std::istream & is,String & s);
};
String operator + (const String &,const String &);
String operator + (const String &,const char *);
String operator + (const char *,const String &);

String::String()
{
        cout<<"String()"<<endl;
        pstr_=new char[1];
        strcpy(pstr_,"");
}
String::~String()
{
        cout<<"~String()"<<endl;
        delete []pstr_;
}
String::String(const char *str)
{
        cout<<"String(const char *)"<<endl;
        pstr_=new char[strlen(str)+1];
        strcpy(pstr_,str);
}
String :: String(const String & s)
{
        cout<<"String(const String &)"<<endl;
        pstr_=new char[strlen(s.pstr_)+1];
        strcpy(pstr_,s.pstr_);
}
String & String::operator = (const String & s)
{
        cout<<"String & operator =(const String &)"<<endl;
        this->~String();
        pstr_=new char[strlen(s.pstr_)+1];
        strcpy(pstr_,s.pstr_);
        return *this;
}
String & String::operator =(const char *s)
{
        cout<<"String & operator=(const char *)"<<endl;
        this->~String();
        pstr_=new char[strlen(s)+1];
        strcpy(pstr_,s);
        return *this;
}
String & String::operator +=(const String &s)
{
        cout<<"String & operator += (const String &)"<<endl;
        int len1=strlen(this->pstr_);
        int len2=strlen(s.pstr_);
        char *a=(char*)calloc(len1,sizeof(char));
        strcpy(a,this->pstr_);
        this->~String();
        this->pstr_=new char[len1+len2+1];
        sprintf(this->pstr_,"%s%s",a,s.pstr_);
        return *this;
}
String & String::operator += (const char *s)
{
        cout<<"String & operator += (const char *)"<<endl;
        int len1=strlen(this->pstr_);
        int len2=strlen(s);
        char *a=(char*)calloc(len1,sizeof(char));
        strcpy(a,this->pstr_);
        this->~String();
        this->pstr_=new char[len1+len2+1];
        sprintf(this->pstr_,"%s%s",a,s);
        return *this;
}
std::ostream &  operator<<(std::ostream & os,const String &s)
{
        cout<<"std::ostream & operator << (std::ostream & ,const String &)"<<endl;
        os<<s.pstr_;
        return os;
}
char& String::operator[] (std::size_t index)
{
        cout<<"char & operator[] (std:: size_t index)"<<endl;
        int len=strlen(this->pstr_);
        char *a=(char*)calloc(len,sizeof(char));
        strcpy(a,pstr_);
        return a[index];
}




const char& String::operator[](std::size_t index)const
{
        cout<<"const char & operator[] (std:: size_t index)const"<<endl;
        int len=strlen(this->pstr_+1);      //This is wrong, this->pstr_+1 is equivalent to correcting the offset to the next character
        char *a=(char*)calloc(len,sizeof(char));
        strcpy(a,pstr_);
        return a[index];
}
std::size_t String::size()const
{
        cout<<"std::size_t size()const"<<endl;
        int len=strlen(this->pstr_);
    return len;
}
const char* String::c_str() const
{
        cout<<"const char* c_str() const"<<endl;
        int len=strlen(this->pstr_+1);
       char *a=(char*)calloc(len,sizeof(char));  
//No matter how much space is applied here, if the later copy function is not enough, he will automatically apply for space, so there is no error in the result
        strcpy(a,pstr_);
        return a;
}
bool operator == (const String & a1,const String &a2)
{
        cout<<"bool operator == (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
bool operator != (const String & a1,const String & a2)
{
        cout<<"bool operator != (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))!=0)
                        return 1;
        return 0;
}
bool operator < (const String &a1,const String &a2)
{
        cout<<"bool operator < (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))<0)
                        return 1;
        return 0;
}
bool operator > (const String &a1,const String &a2)
{
        cout<<"bool operator > (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))>0)
                        return 1;
        return 0;
}
bool operator <= (const String &a1,const String &a2)
{
        cout<<"bool operator <= (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))<0)
                        return 1;
        else if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
bool operator >= (const String &a1,const String &a2)
{
        cout<<"bool operator >= (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))>0)
                        return 1;
        else if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
std::istream & operator >> (std::istream & is,String &s)
{
        cout<<"std::istream & operator >> (std::istream &m,String &)"<<endl;
        is>>s.pstr_;
        return is;
}
String operator + (const String &a1,const String &a2)
{
        cout<<"String operator + (const String &,const String &)"<<endl;
        String tmp=a1;
        tmp+=a2;
        return tmp;
}
String operator + (const String &a1,const char * a2)
{
        cout<<"String operator + (const String &,const char *)"<<endl;
        String tmp=a1;
        tmp+=a2;
        return tmp;
}
String operator + (const char *a1,const String & a2)
{
        cout<<"String operator + (const char *,const String &)"<<endl;
        String tmp;
        tmp+=a1;
        tmp+=a2;
        return tmp;
}

//测试代码
int main()
{
        String s;
        String s1("hello");
        String s2(s1);
        String s3="world";
        s1=s2;
        String s4;
        s4="wangdao";
        s+=s3;
        s4+="nihao";
        cout<<s4<<endl;
        cout<<s4[2]<<endl;
        cout<<s4.size()<<endl;
        cout<<s4.c_str()<<endl;
        bool bool1=(s1>s4);
        cout<<bool1<<endl;
        bool bool2=(s1>=s3);
        cout<<bool2<<endl;
        cin>>s;
        cout<<s<<endl;
        String s5=s1+s2;
        cout<<s5<<endl;
        String s6=s1+"shenzhen";
        cout<<s6<<endl;
        String s7="shenzhen1"+s1;
        cout<<s7<<endl;
        return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324659993&siteId=291194637
Recommended