Files
BasicsOfComputerSoftwareEng…/OOP/13Exam/2.2.cpp
2023-06-02 00:03:58 +08:00

39 lines
707 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 以下程序是定义一个计数器类counter对其重载运算符“+”。请编写运算符重载函数,使得程序输出结果如下:
// n = 5
// n = 10
// n = 15
#include <iostream>
using namespace std;
class counter {
private:
int n;
public:
counter() {
n = 0;
}
counter(int i) {
n = i;
}
//_____________________
// {_____________________}
counter operator+(const counter &otherCounter) {
return counter(this->n + otherCounter.n);
}
void disp() {
cout << "n=" << n << endl;
}
};
int main() {
counter c1(5), c2(10), c3;
c3 = c1 + c2;
c1.disp();
c2.disp();
c3.disp();
return 0;
}