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、思维导图

相关推荐

  1. <span style='color:red;'>C</span>++<span style='color:red;'>作业</span>

    C++作业

    2024-03-16 06:26:02      33 阅读
  2. C++作业

    2024-03-16 06:26:02       31 阅读
  3. <span style='color:red;'>C</span>++<span style='color:red;'>作业</span>

    C++作业

    2024-03-16 06:26:02      25 阅读
  4. <span style='color:red;'>C</span>++<span style='color:red;'>作业</span>

    C++作业

    2024-03-16 06:26:02      31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 06:26:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 06:26:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 06:26:02       82 阅读
  4. Python语言-面向对象

    2024-03-16 06:26:02       91 阅读

热门阅读

  1. 半监督学习--一起学习吧之人工智能

    2024-03-16 06:26:02       42 阅读
  2. 相机学习的知识积累

    2024-03-16 06:26:02       43 阅读
  3. Go 优雅判断 interface 是否为 nil

    2024-03-16 06:26:02       42 阅读
  4. uni-app 安卓手机判断是否开启相机相册权限

    2024-03-16 06:26:02       42 阅读
  5. Thinkphp+workman+redis实现多线程异步任务处理

    2024-03-16 06:26:02       39 阅读
  6. 单例模式详解

    2024-03-16 06:26:02       44 阅读