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; }
相关文章推荐
- 第二周上机实践项目2——C/C++语言中函数参数传递的三种方式
- C++ 虚函数表解析
- 设计模式C++实现(5)——原型模式、模板方法模式
- POJ C++程序设计 编程题#1 输出200
- 字典树 c++ 实现
- C++基础---面向对象程序设计
- 黑马程序员——C语言基本运算
- C++定义的宏
- 项目3 - 体验复杂度-汉诺塔
- 黑马程序员——C语言基础语法
- c++学习(1)
- c++数组操作
- c++数组操作
- 项目3 - 体验复杂度—两种排序算法的运行时间
- c++学习笔记之声明和定义的关系
- [C++] 静态链接库和动态链接库的区别
- C++中的const和define详解
- [C++] static的作用
- windows编程中c语言知识回顾
- C++位操作符总结