作业8.24 运算符重载

该博客详细展示了如何在C++中自定义一个字符串类`my_string`,并实现了关系运算符(>、<、==、>=、<=、!=)和加法运算符(+)的重载。通过实例演示了这些运算符的使用,包括比较字符串和拼接字符串等操作。此外,还包含了拷贝构造函数、赋值运算符重载以及获取字符串长度、是否为空的方法。
摘要由CSDN通过智能技术生成

在my_string的基础上,将能重载的运算符全部重载掉

关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =

实现代码

#include <iostream>
#include <cstring>

using namespace std;
class my_string
{
private:
    char *str;
    int len;
public:
    //无参构造
    my_string()
    {
        str=NULL;
        len=0;
    }
    //有参构造
    my_string(char *str)
    {
        this->str=new char[128];
        strcpy(this->str,str);
        len=strlen(this->str);
    }
    //拷贝构造
    my_string(const my_string &other)
    {
        this->str=new char[128];
        strcpy(this->str,other.str);
        this->len=other.len;
    }
    //拷贝赋值,=运算符重载
    my_string& operator=(const my_string &other)
    {
        if(this->str != NULL)
        {
            delete this->str;            //将原先内存释放,方式出现内存泄露
            this->str = NULL;
        }
        this->str=new char[128];
        strcpy(this->str,other.str);
        this->len=other.len;
    }
    //判空
    bool my_empty()
    {
        if(len==0)
        {
            return 1;
        }
        return 0;
    }
    //求长度
    int my_size()
    {
        return len;
    }
    //转化为c风格字符串
    char *my_str()
    {
        return str;
    }

    //>
    bool operator>(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)>0)
        {
            return 1;
        }
        return 0;
    }

    //<
    bool operator<(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)<0)
        {
            return 1;
        }
        return 0;
    }

    //==
    bool operator==(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)==0)
        {
            return 1;
        }
        return 0;
    }

    //>=
    bool operator>=(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)>=0)
        {
            return 1;
        }
        return 0;
    }

    //<=
    bool operator<=(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)<=0)
        {
            return 1;
        }
        return 0;
    }

    //!=
    bool operator!=(const my_string &R ) const
    {
        if(strcmp(this->str,R.str)!=0)
        {
            return 1;
        }
        return 0;
    }

    //+
    const  my_string  operator+( const  my_string  &R ) const
    {
        my_string str(*this);
        strcat(str.str,R.str);
        str.len+=R.len;
        return str;
    }

    //[]
    const char operator[](const int num)const
    {
        if(num>=0&&num<=len-1)
        {
            return *(this->str+num);
        }
        return '\0';
    }

};

int main()
{
    my_string str1("hello");
    my_string str2("world");
    cout<<boolalpha;
    cout<<"str1:"<<str1.my_str()<<endl;
    cout<<"str2:"<<str2.my_str()<<endl;
    cout<<"str1>str2:"<<(str1>str2)<<endl;
    cout<<"str1<str2:"<<(str1<str2)<<endl;
    cout<<"str1==str2:"<<(str1==str2)<<endl;
    cout<<"str1>=str2:"<<(str1>=str2)<<endl;
    cout<<"str1<=str2:"<<(str1<=str2)<<endl;
    cout<<"str1!=str2:"<<(str1!=str2)<<endl;
    my_string str3=str1+str2;
    cout<<"str1+str2="<<str3.my_str()<<endl;
    cout<<"str1[0]="<<str1[0]<<endl;
    cout<<"str2[4]="<<str2[4]<<endl;

    return 0;
}

运行结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不知名大学生M

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

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

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

打赏作者

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

抵扣说明:

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

余额充值