C++day4

 运算符重载

 

#include <iostream>
#include <cstring>

using namespace std;

class myString;

class myString
{
private:
    char* str;
    int size;
public:
    myString():size(10)
    {
        str = new char(size);
        strcpy(str,"");
        cout<<"无参构造"<<endl;
    }
    myString(char* s)
    {
        size = strlen(s);
        str = new char (size+1);
        strcpy(str,s);
        cout<<"有参构造"<<endl;
    }
    //析构函数
    ~myString()
    {
        delete []str;
        cout<<"析构函数"<<endl;
    };

    const myString &operator=(const myString&n)
    {
        if (this != &n)
        {
            delete []this->str;
            new char (n.size);
            strcpy (this->str, n.str);
            this->size = n.size;
        }
        return *this;
    }

    const myString operator+(const myString&m)const
    {
        myString temp;
        temp.size = this->size+m.size;
        temp.str = new char(temp.size+1);
        memset(temp.str,0,temp.size+1);
        strcat(temp.str,this->str);
        strcat(temp.str,m.str);
        cout<<"连接运算符"<<endl;
        return temp;
    }

    const myString operator+=(const myString&m)
    {
        myString temp;
        temp.size = this->size+m.size;
        temp.str = new char(temp.size+1);
        strcat(this->str,m.str);
        strcat(temp.str,this->str);
        cout<<"+=运算符"<<endl;
        return temp;
    }


    char &operator[](size_t i)
    {
        if (i >= strlen(str))
        {
            cout<<"异常"<<endl;
        }else
        {
            return str[i];
        }
    }

    bool operator>(const myString&k)
    {
        if (strcmp(this->str,k.str) > 0)
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return false;
        }else
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return true;
        }
    }

    bool operator<(const myString&k)
    {
        if (strcmp(this->str,k.str) < 0)
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return false;
        }else
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return true;
        }
    }

    bool operator==(const myString&k)
    {
        if (strcmp(this->str,k.str) == 0)
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return true;
        }else
        {
            cout<<this->str<<"   "<<k.str<<endl;
            return false;
        }
    }

    friend ostream &operator<<(ostream &L, const myString &K);
    friend istream &operator>>(istream &L, myString &K);
    void show(void)const
    {
        cout<<str<<endl;
    }

};

ostream &operator<<(ostream &L, const myString &K)
{
    L<<K.str<<endl;
    return L;
}

istream &operator>>(istream &L, myString &K)
{
    L>>K.str;
    return L;
}
int main()
{
    char s1[128] = "";              //s1 = 123
    char s2[128] = "";              //s2 = 456
    char s3[128] = "";
    cout<<"请输入第一个字符串";
    cin>>s1;
    cout<<"请输入第一个字符串";
    cin>>s2;
    myString str1(s1);
    myString str2(s2);
    myString str3(s3);
    myString str4(s3);
    myString str5;
    str1.show();            // 此时s1 = 123
    str2.show();            // 此时s2 = 456
    cout<<str1+str2<<endl;       //s4 = 123465
    str3 = str2;            //s3 = 465
    //str3 = str1;            //s3 = 123465
    str3.show();
    if (str3 > str1)
    {
        cout<<"yes"<<endl;
    }else
    {
        cout<<"no"<<endl;
    }

    if (str3 < str1)
    {
        cout<<"yes"<<endl;
    }else
    {
        cout<<"no"<<endl;
    }

    if (str4 == str1)
    {
        cout<<"yes"<<endl;
    }else
    {
        cout<<"no"<<endl;
    }
    str1[2] = 'M';
    str1.show();        //此时str1的值为125456

    cout<<"请输入str5>>>";
    cin>>str5;
    cout<<" str5 = "<<str5<<endl;
    str5 += str5;
    cout<<" str5 = "<<str5<<endl;
    system("pause");


    return 0;
}

 

思维导图

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值