C++--String类型各种重载函数的实现

#include 
#include 
#include 
#include 



using namespace std;

class 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 bool operator >= (const String &,const String &);

        friend ostream &operator <<(ostream &os,const String &s);
        friend istream &operator >>(istream &os,String &s);


public:
        //无参构造函数
        String()
        {
         pstr_=new char[1];
        }

        //有参构造函数
        String (const char *s)
        {
         pstr_=new char[strlen(s)+1];
         strcpy(pstr_,s);
        }


        //复制构造函数
        String (const String &rhs)
        {
          pstr_=new char[strlen(rhs.pstr_)+1];
          strcpy(pstr_,rhs.pstr_);
        }


        //赋值对象重载函数
        String &operator = (const String &rhs)
        {
         if(this!=&rhs)//如果不是自复制
         {
          delete []pstr_;//先删除掉原有的内容
          pstr_=new char[strlen(rhs.pstr_)+1];
          strcpy(pstr_,rhs.pstr_);
         }
         return *this;
        }


        //赋值字符串重载函数
        String &operator = (const char *s)
        {
         pstr_=new char[strlen(s)+1];
         strcpy(pstr_,s);
         return *this;
        }


        //重载+=运算符  左边对象本身发生了改变
        String & operator +=(const String & rhs)
        {
         //用临时的字符串指针存放
         char *tmp;
         tmp=new char[strlen(pstr_)+strlen(rhs.pstr_)+1];
         strcpy(tmp,pstr_);
         strcat(tmp,rhs.pstr_);
         delete []pstr_;
         pstr_=tmp;
         return *this;

        }
        String &operator += (const char *pstr)
        {
         char *tmp;
         tmp = new char[strlen(pstr_)+strlen(pstr)+1];//先开空间
         strcpy(tmp,pstr_);
         strcat(tmp,pstr);
         delete []pstr_;
         pstr_=tmp;
         return *this;
        }


        //重载[]下标运算符
        char &operator[](size_t index)
        {
         static char sNULL='\0';
         //要判断给出的下标越界的情况
         if(index0)
         {
          return pstr_[index];
         }
         else
         {
          cout<<"下标越界"<0)
         {
          return pstr_[index];
         }
         else
         {
          cout<<"下标越界"<函数
bool operator >(const String &lhs,const String &rhs)
{
        if(strcmp(lhs.pstr_,rhs.pstr_)>0)
        {
         return 1;
        }
        else
        {
         return 0;
        }
}
//重载<=函数
bool operator <=(const String &lhs,const String &rhs)
{
        if(strcmp(lhs.pstr_,rhs.pstr_)<=0)
        {
         return 1;
        }
        else
        {
         return 0;
        }
}

//重载>=函数
bool operator >=(const String &lhs,const String &rhs)
{
   if(strcmp(lhs.pstr_,rhs.pstr_)>=0)
        {
         return 1;
        }
        else
        {
         return 0;
        }
}

//重载<<流
ostream &operator <<(ostream &ofs,const String &rhs)
{
        ofs<>流
istream &operator >>(istream &ifs, String &rhs)
{

        char tmp[1024];//先开空间
        ifs>>tmp;
        rhs.pstr_=tmp;//输入的字符 放到rhs中
        return ifs;

}
//重载+(两个对象之间)
String operator +(const String &lhs,const String & rhs)
{
        String str(lhs);
        str+=rhs;
        return str;
}
String operator +(const String &lhs,const char *s)
{
        String str(lhs);
        str+=s;
        return str;
}
String operator +(const char *s,const String &rhs)
{
        String str(rhs);
        str+=s;
        return s;

}


int main()
{

        String s1;//无参函数
        s1.print();

        String s2="hello";//有参函数
        s2.print();

        String s3=s2;//调用复制构造函数
        s3.print();

        String s4="world";//赋值对象重载函数
        s4=s2;
        s4.print();

        String s5; //赋值字符串重载函数
        s5="hello";
        s5.print();


        String s6="hello";//重载+=函数
        String s7="world";
        s6+=s7;
        s6.print();

        String s8="hello";//重载++函数
        s8+="world";
        s8.print();

        s8[3]='A';
        cout<>
        cin>>s14;
        cout<

你可能感兴趣的:(C)