C++>运算符重载>day3

作业

运算符重载掉

        关系运算符:>、>>、<<

        加号运算符:+

        取成员运算符:[]

        赋值运算符: =

一、代码

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class node
{
public:
    //无参构造
    //有参构造
    //拷贝构造
    //拷贝赋值
    //bool my_empty()        判空
    //int my_size()          求长度
    //char *my_str()         转化为c风格字符串
    ~node(){
        cout<<"调用析构函数成功"<<endl;
    }
    node (){}
    node(int s,int l)
    {
        str=s;
        len=l;
    }
    node(node &Q)
    {
        str=Q.str;
        this->len=Q.len;
    }
    void put()
    {
        cout<<str<<'\t'<<len<<endl;
    }

    //>
    friend bool operator>(node &Q,node &R);
    //+
    friend node operator+(node &Q,node &R);
    //=
    friend node operator+(node &Q);
    //<<
    friend ostream& operator<<(ostream &output, const node &c);
    friend istream& operator>>(istream &input,node &c);

private:
    int str;
    int len;
};

//>
bool operator>(node &Q,node &R)
{
    if(Q.str>R.str && Q.len>R.len)
    {
        return true;
    }else
    {
        return false;
    }
}
//+
node operator+(node &Q,node &R)
{
    node temp;
    temp.str=Q.str+R.str;
    temp.len=Q.len+R.len;
    return temp;
}
//=
node operator+(node &Q)
{
    node temp;
    temp.str=Q.str;
    temp.len=Q.len;
    return temp;
}
//<<
ostream& operator<<(ostream &output,const node &c)
{
    output<<"("<<c.str<<","<<c.len<<"i)";
    return output;
}
//>>
istream& operator>>(istream &input,node &c)
{
    cout<<"请输入\n";
    input>>c.str>>c.len;
    return input;
}
int main()
{
    cout<<"str"<<'\t'<<"len"<<endl;
    node s1(1,2),s2,s3;
    s2=s1;
    s1.put();
    s2.put();
    s2=s1+s2;
    s2.put();
    cout<<s2<<endl;
    cin>>s3;
    cout<<s3<<endl;



    return 0;
}

二、执行结果 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值