22.12.7 在My_ string类中 ,将能实现重载的运算符,尽可能重载

#include <iostream>
#include <string>

using namespace std;
class my_string
{
private:
    char *str;                  //字符串指针
    int len;                    //长度
public:
    my_string(){}           //无参构造函数
    ~my_string(){}              //析构函数
    my_string(char *p)          //有参构造函数
    {

        len=my_size(p);
        str=new char[len+1];        //地址
        for(int i=0;i<len+1;i++)
        {
            str[i]=*(p+i);       //赋值
        }
    }
    my_string(my_string& s)      //拷贝构造函数  同类的类对象
    {
        this->len=s.len;
        str=new char[len+1];
        for(int i=0;i<len+1;i++)
        {
            str[i]=s.str[i];
        }
    }

    bool operator == (my_string& S)
    {
        int i;
        for( i=0;i<len+1;i++)
        {
            if(str[i]!=S.str[i])
            {
                break;
            }
        }
        if((this->len==S.len)&&(i==this->len))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool operator >(my_string&S)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=S.str[i])
            {
                break;
            }
        }
        if(str[i]>S.str[i])
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool operator <(my_string&S)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=S.str[i])
            {
                break;
            }
        }
        if(str[i]<S.str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator >=(my_string&S)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
           if(str[i]!=S.str[i])
           {
               break;
           }
        }
        if(str[i]>=S.str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator <=(my_string&S)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=S.str[i])
            {
                break;
            }
        }
        if(str[i]<=S.str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator !=(my_string&S)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=S.str[i])
            {
                break;
            }
        }
        if((this->len==S.len)&&(i==this->len))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    void show()
    {
        cout<<"str="<<this->str<<" yu"<<endl;
    }
    int my_size(char *S)
    {
        int len=0;
        while(*S!='\0')
        {
            len++;
            S++;
        }
        return len;
    }
    bool my_empty()
    {
        return len==0?true:false;
    }
    char my_at(int i)
    {
        return str[i];
    }
};

int main()
{
    char c[]="yuyu so pretty wubalubadubdub";
    char *q;
    q=c;
    my_string s1(q);
    s1.show();
    my_string s2(s1);
    s2.show();
    my_string s3;
    s3=s2;
    s3.show();
    if(s1.my_empty()==true)
    {
        cout<<"null"<<endl;
    }
    else
    {
        cout<<"full"<<endl;
    }
    char a[]="yuyu so pretty wubalubadubdub";
    char *p;
    p=a;
    my_string s4(p);
    s4.show();
    cout<<(s1==s4?"1":"0")<<endl;
    cout<<(s1>s4?"1":"0")<<endl;
    cout<<(s1<s4?"1":"0")<<endl;
    cout<<(s1>=s4?"1":"0")<<endl;
    cout<<(s1<=s4?"1":"0")<<endl;
    cout<<(s1!=s4?"1":"0")<<endl;

    cout<<"动感光波~~~哔~哔~哔~哔~~~"<<endl;

    return 0;
}

 

 

 

#include <iostream>
#include <string>

using namespace std;
class my_string
{
private:
    char *str;                  //字符串指针
    int len;                    //长度
public:
    my_string(){}           //无参构造函数
    ~my_string(){}              //析构函数
    my_string(char *p)          //有参构造函数
    {

        len=my_size(p);
        str=new char[len+1];        //地址
        for(int i=0;i<len+1;i++)
        {
            str[i]=*(p+i);       //赋值
        }
    }
    my_string(my_string& s)      //拷贝构造函数  同类的类对象
    {
        this->len=s.len;
        str=new char[len+1];
        for(int i=0;i<len+1;i++)
        {
            str[i]=s.str[i];
        }
    }

    bool operator == (my_string& s)
    {
        int i;
        for( i=0;i<len+1;i++)
        {
            if(str[i]!=s.str[i])
            {
                break;
            }
        }
        if((this->len==s.len)&&(i==this->len))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool operator >(my_string& s)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=s.str[i])
            {
                break;
            }
        }
        if(str[i]>s.str[i])
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool operator <(my_string& s)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=s.str[i])
            {
                break;
            }
        }
        if(str[i]<s.str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator >=(my_string& s)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
           if(str[i]!=s.str[i])
           {
               break;
           }
        }
        if(str[i]>=s.str[i])
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool operator <=(my_string& s)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=s.str[i])
            {
                break;
            }
        }
        if(str[i]<=s.str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator !=(my_string& s)
    {
        int i;
        for(i=0;i<len+1;i++)
        {
            if(str[i]!=s.str[i])
            {
                break;
            }
        }
        if((this->len==s.len)&&(i==this->len))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    void show()
    {
        cout<<"str->"<<this->str<<" yu"<<endl;
    }
    int my_size(char *s)
    {
        int len=0;
        while(*s!='\0')
        {
            len++;
            s++;
        }
        return len;
    }
    bool my_empty()
    {
        return len==0?0:1;
    }
    char my_at(int i)
    {
        return str[i];
    }
};

int main()
{
    char c[]="yuyu so pretty wubalubadubdub";
    char *q;
    q=c;
    my_string s1(q);
    cout<<"s1.str如下:"<<endl;
    s1.show();
    my_string s2(s1);
    cout<<"s2.str如下:"<<endl;
    s2.show();
    my_string s3;
    cout<<"s3.str如下:"<<endl;
    s3=s2;
    s3.show();
    char a[]="yuyu so pretty wubalubadubdub";
    char *p;
    p=a;
    my_string s4(p);
    cout<<"s4.str如下:"<<endl;
    s4.show();
    if(s1.my_empty()==0)
    {
        cout<<"s1为空 输出:null"<<endl;
    }
    else
    {
        cout<<"s1不为空 输出:full"<<endl;
    }
    cout<<"s4.str如下:"<<endl;
    s4.show();
    cout<<"s1=s4 是不是: "<<(s1==s4?"Y":"N")<<endl;
    cout<<"   "<<endl;
    cout<<"s1>s4 是不是: "<<(s1>s4?"Y":"N")<<endl;
    cout<<"   "<<endl;
    cout<<"s1<s4 是不是: "<<(s1<s4?"Y":"N")<<endl;
    cout<<"  "<<endl;
    cout<<"s1>=s4 是不是: "<<(s1>=s4?"Y":"N")<<endl;
    cout<<"  "<<endl;
    cout<<"s1<=s4 是不是: "<<(s1<=s4?"Y":"N")<<endl;
    cout<<"  "<<endl;
    cout<<"s1!=s4 是不是: "<<(s1!=s4?"Y":"N")<<endl;
    cout<<"  "<<endl;
    cout<<"  "<<endl;
    cout<<"动感光波~~~哔~哔~哔~哔~~~"<<endl;

    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值