8.24嵌入式作业(运算符重载)

作业内容

在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =

代码部分

#include <iostream>
#include <cstring>

using namespace std;

class my_string
{
private:
    char *str;
    int len;
public:
    friend const bool operator >(const my_string&, const my_string&);
    friend const bool operator <(const my_string&, const my_string&);
    friend const bool operator ==(const my_string&, const my_string&);
    friend const bool operator >=(const my_string&, const my_string&);
    friend const bool operator <=(const my_string&, const my_string&);
    friend const bool operator !=(const my_string&, const my_string&);
    //friend const my_string& operator +(my_string&, const my_string&);
    //无参构造
    my_string()
    {
        str = new char[128];
        memset(str, 0, 128);
        len = strlen(str);
        //cout << "无参构造函数" << endl;
    }

    //有参构造
    my_string(char *str)
    {
        this->str = new char[128];
        strcpy(this->str, str);
        len = strlen(str);
        //cout << "有参构造函数" << endl;
    }

    //拷贝构造
    my_string(const my_string &other)
    {
        str = new char[128];
        memset(str, 0, 128);
        strcpy(str, other.str);
        len = other.len;
        //cout << "拷贝构造函数" << endl;
    }

    //拷贝赋值
    my_string& operator =(const my_string &other)
    {
        str=new char[128];
        memset(str, 0, 128);
        strcpy(str,other.str);
        len=other.len;
    }

    ~my_string()
    {
        delete str;
    }

    //显示函数
    void display()
    {
        cout << str << '\t' << len << endl;
    }

    //判空
    bool my_empty()
    {
        return strlen(str)==0?true:false;
    }

    //求长度
    int my_size()
    {
        return strlen(str);
    }

    //转化为c风格字符串
    char *my_str()
    {
        return str;
    }

    //加号运算符
    my_string operator +(const my_string& O)const
    {
        my_string temp;
        strcpy(temp.str, this->str);
        strcat(temp.str, O.str);
        temp.len = this->len + O.len;
        return temp;
    }

    //取成员运算符
    const char operator [](const int index)
    {
        return this->str[index-1];
    }

    //    //赋值运算符
    //    const my_string& operator =(const my_string& R)
    //    {
    //        strcpy(this->str, R.str);
    //        return *this;
    //    }

};

//关系运算符
const bool operator >(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) > 0)
        return true;
    else
        return false;
}
const bool operator <(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) < 0)
        return true;
    else
        return false;
}
const bool operator ==(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) == 0)
        return true;
    else
        return false;
}
const bool operator >=(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) >= 0)
        return true;
    else
        return false;
}
const bool operator <=(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) <= 0)
        return true;
    else
        return false;
}
const bool operator !=(const my_string& L, const my_string& R)
{
    if(strcmp(L.str,R.str) != 0)
        return true;
    else
        return false;
}

//加号运算符
//const my_string& operator +(my_string& L, const my_string& R)
//{
//    strcat(L.str, R.str);
//    L.len = L.len + R.len;
//    return L;
//}

int main()
{
    //    my_string ms1;

    //    char* str = "zhangying";
    //    my_string ms2(str);
    //    cout << ms2.my_empty() << endl;
    //    ms2.display();

    //    my_string ms3(ms2);
    //    cout << ms3.my_empty() << endl;
    //    ms3.display();

    //    my_string ms4 = ms3;
    //    cout << ms4.my_empty() << endl;
    //    ms4.display();
    my_string ms1("zhang");
    my_string ms2("ying");
    //关系运算符
    cout << "ms1>ms2:" << (ms1>ms2) << endl;
    cout << "ms1<ms2:" << (ms1<ms2) << endl;
    cout << "ms1==ms2:" << (ms1==ms2) << endl;
    cout << "ms1>=ms2:" << (ms1>=ms2) << endl;
    cout << "ms1<=ms2:" << (ms1<=ms2) << endl;
    cout << "ms1!=ms2:" << (ms1!=ms2) << endl;
    //加号运算符
    my_string ms3=ms1+ms2;
    ms3.display();
    cout << ms3.my_str()[2] << endl;

    return 0;
}


测试结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值