您的位置:首页 > 编程语言 > C语言/C++

POJ C++程序设计 编程题#2 输出指定结果一

2015-09-13 16:48 281 查看

编程题#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;
}

输入

不需要输入。

输出

使输出结果为

2

2

8

10

样例输入

不需要输入。


样例输出

2
2
8
10


#include <iostream>
using namespace std;
class Number {
public:
int num;
Number(int n): num(n) {
}
// 在此处补充你的代码
int &value() {
return num;
}
void operator+(Number &n) {
this->num += n.num;
}
};
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: