POJ C++程序设计 编程题#2 输出指定结果一
编程题#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
1 #include <iostream> 2 using namespace std; 3 class Number { 4 public: 5 int num; 6 Number(int n): num(n) { 7 } 8 // 在此处补充你的代码 9 int &value() { 10 return num; 11 } 12 void operator+(Number &n) { 13 this->num += n.num; 14 } 15 }; 16 int main() { 17 Number a(2); 18 Number b = a; 19 cout << a.value() << endl; 20 cout << b.value() << endl; 21 a.value() = 8; 22 cout << a.value() << endl; 23 a+b; 24 cout << a.value() << endl; 25 return 0; 26 }
                    
                
                
            
        
浙公网安备 33010602011771号