第十六周 阅读程序一(3)
2015-06-17 14:26
134 查看
问题及代码:
运行结果:
学习心得:
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
因为using CounterNameSpace::upperbound;已经明确了upperbound的命名空间。
(b)(c)处:是否可以省去CounterNameSpace::?
不可以省去,省去就会发生命名冲突。
#include <iostream> using namespace std; namespace CounterNameSpace { int upperbound; int lowerbound; class counter { int count; public: counter(int n) { if (n <= upperbound ) { count = n; } else { count = upperbound; } } void reset(int n) { if (n < upperbound) { count = n; } } int run() { if (count > lowerbound) { return count--; } else return lowerbound; } }; } int main() { using CounterNameSpace::upperbound; upperbound = 100; //(a) CounterNameSpace::lowerbound = 0; //(b) CounterNameSpace::counter ob1(10); int i; do { i = ob1.run(); cout << i<<" "; } while( i > CounterNameSpace::lowerbound); cout << endl; using namespace CounterNameSpace; counter ob2(20); do { i = ob2.run(); cout << i<<" "; } while( i > CounterNameSpace::lowerbound); //(c) cout << endl; ob2.reset(100); lowerbound = 90; //(d) do { i = ob2.run(); cout << i <<" "; } while( i > lowerbound); return 0; }
运行结果:
学习心得:
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
因为using CounterNameSpace::upperbound;已经明确了upperbound的命名空间。
(b)(c)处:是否可以省去CounterNameSpace::?
不可以省去,省去就会发生命名冲突。
相关文章推荐
- 再谈 最速下降法/梯度法/Steepest Descent
- 深度学习-LeCun、Bengio和Hinton的联合综述
- Java NIO系列教程(十一) Pipe
- jQuery 学习笔记 基础篇六 jQuery事件与应用
- 数据类型及转换
- 图片文字不间断滚动
- Asp: Server.mapPath() 注意事项
- 再谈 最速下降法/梯度法/Steepest Descent
- mtk 驱动笔记 lcd camera led tp
- 中国CDN行业,谁主沉浮?
- 七牛的存储算法猜测
- 关于防盗链和镜像源的域名配置详解。
- Android应用程序中分享图片和文字给好友
- 检测网页是否通过W3C标准的校验
- 二,mogodb索引
- SSL协议详解
- 找到netstat的程序的名称
- Android系统性能调优工具介绍
- 再谈 共轭方向法/Conjugate Direction Method In Optimization
- Asp.net MVC CSS/Javascript Bundle 配置文件