C++ 作业 24/3/14

1、成员函数版本实现算术运算符的重载;全局函数版本实现算术运算符的重载

#include <iostream>

using namespace std;

class Test
{
    friend const Test operator-(const Test &L,const Test &R);
private:
    int c;
    int n;
public:
    Test(){}
    Test(int c,int n):c(c),n(n)
    {}
    //类内成员函数实现+运算符重载
    const Test operator+(const Test &T)const
    {
        Test temp;
        temp.c = c+T.c;
        temp.n = n+T.n;
        return temp;
    }

    void show()
    {
        cout << "c = " << c << "n = " << n << endl;

    }
};
//全局函数实现—运算符重载
const Test operator-(const Test &L,const Test &R)
{
    Test temp;
    temp.c = L.c-R.c;
    temp.n = L.n-R.n;
    return temp;
}

int main()
{
    Test t1('K',4);
    Test t2('Y',8);
    Test t3 = t1 + t2;
    t3.show();
    Test t4 = t1 - t2;
    t4.show();
    return 0;
}

2、思维导图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值