编程题#2:输出指定结果一
来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
总时间限制: 1000ms 内存限制: 1024kB
描述
填写代码,使输出结果为
2
2
8
10
#include <iostream>
using namespace std;
class Number {
public:
int num;
Number(int n): num(n) {
}
// 在此处补充你的代码
};
int main() {
Number a(2);
Number b = a;
cout << a.value() << endl;
cout << b.value() << endl;
a.value() = 8;
cout << a.value() << endl;
a+b;
cout << a.value() << endl;
return 0;
}
-------------以下是填充的代码---------------
Number& operator=(const Number& n)
{
num = n.num;
}
Number&operator+(const Number &n) {
num += n.num;
return *this;
}
int & value() { return num; }