13年题。

This commit is contained in:
unlockable
2023-06-02 00:03:58 +08:00
parent 955977fef2
commit 9cecf352c2
11 changed files with 401 additions and 0 deletions

39
OOP/13Exam/2.2.cpp Normal file
View File

@@ -0,0 +1,39 @@
// 以下程序是定义一个计数器类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;
}