39 lines
707 B
C++
39 lines
707 B
C++
// 以下程序是定义一个计数器类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;
|
||
} |