#include using namespace std;

class Counter { private: int n; public: Counter(int num) : n(num) {}

Counter operator+(const Counter& other) {
    Counter result(n + other.n);
    return result;
}

int getValue() {
    return n;
}

};

int main() { Counter c1(5); Counter c2(10);

Counter c3 = c1 + c2;

cout << 'c1 + c2 = ' << c3.getValue() << endl;

return 0;

}

C++ 计数器类:重载运算符 '+' 实现对象相加

原文地址: https://www.cveoy.top/t/topic/f31c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录