2022/8/24——在昨天的my_string类的基础上进行重载运算符

使用运算符重载,能够使得代码更加简洁 。

所谓运算符重载,本质上也还是函数的重载,能够实现“一符多用”,将原本只是针对于基本数据类型的一些运算符,赋予类对象之间的运算,使得类对象之间操作更加优雅。

重载的方法:函数名的统称:operator# (#表示要被重载的运算符,例如:加减乘除) 

关系运算符的重载

1)>的重载

bool operator>(const my_string & R,const my_string & L)
{
    int res = 0;
    int rlen =(R.len>L.len)? R.len:L.len;      //取出两字符串较长的长度
    for(int i=0; i<rlen;i++)          //依次取出两字符串的字符相比较
    {
        res=R.str[i]-L.str[i];        //以ascll码判断大小
        if(res>0)
        {
            return true;
        }
        else if(res<0)
        {
            return false;
        }
    }
    return false;
}

2)<的重载

bool operator<(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res<0)
        {
            return true;
        }
        else if(res>0)
        {
            return false;
        }
    }
    return false;
}

3)==的重载

bool operator==(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res<0 || res >0)
        {
            return false;
        }
    }
    return true;
}

4)>=的重载

bool operator>=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0)
        {
            return true;
        }
        else if(res<0)
        {
            return false;
        }
    }
    return true;
}

5)<=的重载

bool operator<=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0)
        {
            return false;
        }
        else if(res<0)
        {
            return true;
        }
    }
    return true;
}

6)!=的重载

bool operator!=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0 || res<0)
        {
            return true;
        }
    }
    return false;
}
加号运算符的重载(+)
my_string operator+(my_string & R,my_string & L)
{
    my_string temp;
    temp.len = R.len+L.len;
    temp.str = new char[temp.len+1];
    for(int i=0; i<R.len; i++)
    {
        temp.str[i]=R.str[i];
    }
    for(int j=0; j<L.len; j++)
    {
        temp.str[R.len+j]=L.str[j];
    }
    return temp;
}
取成员运算符[]重载
    char & operator [](int i)
    {
        return str[i];
    }

完整代码

#include <iostream>
#include <cstring>
using namespace std;

class my_string
{
public:
    //无参构造
    my_string()
    {
        str=new char[1];
        *str='\0';
        len = 0;
    }
    //有参构造
    my_string(const char *s)
    {
        len = strlen(s);    //获取字符串长度
        str = new char[len+1];   //申请足够的空间存放字符串
        strcpy(str,s);     //拷贝内容
    }
    my_string(int a, char c)
    {
        str = new char[a+1];
        for(int i=0;i<a;i++)
        {
            str[i]= c;
        }
        len = a;
    }
    //拷贝构造
    my_string(const my_string& S)   //深拷贝
    {
        str = new char[len+1];
        strcpy(str,S.str);
        this->len = S.len;
    }
    //拷贝赋值
    my_string& operator=(const my_string& R)
    {
        if(this!=&R)
        {
            //this->str = R.str;    //浅拷贝
            this->str = new char[1];
            strcpy(this->str,R.str);
            this->len = R.len;
        }
        return *this;
    }

    //将运算符重载
    //>
    friend bool operator>(const my_string&,const my_string&);
    //<
    friend bool operator<(const my_string&,const my_string&);
    //==
    friend bool operator==(const my_string&,const my_string&);
    //>=
    friend bool operator>=(const my_string&,const my_string&);
    //<=
    friend bool operator<=(const my_string&,const my_string&);
    /*!=*/
    friend bool operator!=(const my_string&,const my_string&);
    //+
    friend my_string operator+(my_string&,my_string&);

    //将取成员运算符[]重载
    char & operator [](int i)
    {
        return str[i];
    }

    //判空
    bool my_empty()
    {
        if(*str == 0)
            return true;
        else
            return false;
     }
     //求长度
     int my_size()
     {
        int size = 0;
        char *p = str;
        while(*p++ != '\0'){ size++; }
        return size;
     }
     //转化为c风格字符串
     char *my_str()
     {
        return str;
     }

     void display()
     {
         cout<<"str:"<<str<<'\t'<<"len:"<<len<<endl;
     }
private:
    char *str;
    int len;
};
//关系运算符>的重载
bool operator>(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0)
        {
            return true;
        }
        else if(res<0)
        {
            return false;
        }
    }
    return false;
}
//关系运算符<的重载
bool operator<(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res<0)
        {
            return true;
        }
        else if(res>0)
        {
            return false;
        }
    }
    return false;
}
//关系运算符==的重载
bool operator==(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res<0 || res >0)
        {
            return false;
        }
    }
    return true;
}
//关系运算符>=的重载
bool operator>=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0)
        {
            return true;
        }
        else if(res<0)
        {
            return false;
        }
    }
    return true;
}
//关系运算符<=的重载
bool operator<=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0)
        {
            return false;
        }
        else if(res<0)
        {
            return true;
        }
    }
    return true;
}
//关系运算符!=的重载
bool operator!=(const my_string & R,const my_string & L)
{
    int res=0;
    int rlen =(R.len>L.len)? R.len:L.len;
    for(int i=0; i<rlen;i++)
    {
        res=R.str[i]-L.str[i];
        if(res>0 || res<0)
        {
            return true;
        }
    }
    return false;
}
//加号运算符(实现的效果类似于c语言中的strcat函数)
my_string operator+(my_string & R,my_string & L)
{
    my_string temp;
    temp.len = R.len+L.len;
    temp.str = new char[temp.len+1];
    for(int i=0; i<R.len; i++)
    {
        temp.str[i]=R.str[i];
    }
    for(int j=0; j<L.len; j++)
    {
        temp.str[R.len+j]=L.str[j];
    }
    return temp;
}
int main()
{
    my_string s1("hqyj");
    my_string s2;
    s2 = s1;
    cout<<(s1==s2)<<endl;
    my_string s4("hqyja");
    cout<<(s1==s4)<<endl;
    s4.display();
    my_string s5=s1+s4;
    s5.display();
    cout<<s1[2]<<endl;
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

命如星火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值