2022/8/24---C++day4

创作本文目的:记录自己的学习历程


一、任务

1.要求

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

2.代码

#include <iostream>

using namespace std;

class my_string
{
private:
    char *str;
    int len;
public:
    //无参构造
    my_string(){
        cout<<"无参构造"<<endl;
        this->str=new char[512];
        this->len=0;
    }
    //有参构造
    my_string(char *s,int l){
        cout<<"有参构造"<<endl;
        len = l;
        str = new char[len+1];
        memcpy(this->str,s,sizeof(char[len]));
        str[len]=0;
    }
    //拷贝构造
    my_string(const my_string &s){
        len = s.len;
        str = new char[len+1];
        memcpy(this->str,s.str,sizeof(char[len]));
        str[len]=0;
    }
    //拷贝赋值   赋值运算符重载
    my_string &operator=(const my_string &s){
        len = s.len;
        str = new char[len+1];
        memcpy(this->str,s.str,sizeof(char[len]));
        str[len]=0;
        return *this;
    }
    //关系运算符重载
    bool operator<(my_string &s){
        int i=0;
        while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
        {
            i++;
        }
        if(*(str+i)<*(s.str+i))
            return true;
        else
            return false;
    }

    bool operator>(my_string &s){
        int i=0;
        while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
        {
            i++;
        }
        if(*(str+i)>*(s.str+i))
            return true;
        else
            return false;
    }

    bool operator==(my_string &s){
        int i=0;
        while(*(str+i)&& *(s.str+i) && *(str+i)==*(s.str+i))
        {

            i++;
        }
        if(*(str+i)==*(s.str+i))
            return true;
        else
            return false;
    }

    bool operator!=(my_string &s){
        int i=0;
        while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
        {
            i++;
        }
        if(*(str+i)!=*(s.str+i))
            return true;
        else
            return false;
    }

    bool operator<=(my_string &s){
        int i=0;
        while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
        {
            i++;
        }
        if(*(str+i)<=*(s.str+i))
            return true;
        else
            return false;
    }

    bool operator>=(my_string &s){
        int i=0;
        while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
        {
            i++;
        }
        if(*(str+i)>=*(s.str+i))
            return true;
        else
            return false;
    }

    //加号运算符重载
    my_string& operator+(const my_string &s)
    {
        my_string tem;
        char *q=tem.str;
        char *y=this->str;
        char *x=s.str;
        int cout=0;
        while(*q=*y)
        {
            q++;
            y++;
            cout++;
        }
        while(*q=*(x++))
        {
            q++;
            cout++;
        }
        tem.len=cout;
        return tem;
    }

    //取成员运算符重载
    char operator[](int n)
    {
        if(n>len)
        {
            cout<<"数组越界"<<endl;
            return '0';
        }
        return str[n];
    }

    //提取运算符重载
    friend ostream& operator<<(ostream& ,my_string&);

    //判空  空为真,非空为假
    bool my_empty()
    {
        if(*str=='\0')
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    //求长度
    int my_size()
    {
        char *q=str+len;
        while(*q!='\0')
        {
            len++;
            q++;
        }
        return len;
    }

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

    void display()
    {
        for(int i=0;i<len;i++)
        {
            cout<<*(str+i);
        }
        cout<<endl;
    }
    ~my_string(){
//        delete str;
//        str=NULL;
    }
};

ostream& operator<<(ostream& output,my_string& s)
{
    char* q=s.str;
    int i=0;
    while(*q!='\0')
    {
        output<<*(s.str+i);
        q++;
        i++;
    }
    return output;
}

int main()
{
    my_string s("hello world",11);
    cout<<s.my_str()<<endl;

    my_string s1(s);
    s1.display();
    my_string s2;
    s2 = s1;
    s2.display();
    cout<<s2.my_size()<<endl;

    my_string s3("",0);
    if(s3.my_empty())
    {
        cout<<"空"<<endl;
    }
    else
    {
        cout<<"非空"<<endl;
        s3.display();
    }
    my_string s4("hello zorld",11);
    cout<<(s1>s2?"yes":"no")<<endl;
    cout<<(s1==s2?"yes":"no")<<endl;
    cout<<(s4>=s2?"yes":"no")<<endl;

    s3=s1+s4;
    cout<<s1+s2<<endl;
    s3.display();
    s1.display();
    cout<<s3[2]<<endl;
    cout<<s3<<endl;
    return 0;
}

3.运行效果

在这里插入图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值