第十六周 程序阅读(3)
来源:互联网 发布:魔兽网络代理 编辑:程序博客网 时间:2024/06/05 10:47
问题及代码:
代码(2)
#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(){ CounterNameSpace::upperbound = 100; CounterNameSpace::lowerbound = 0; CounterNameSpace::counter ob1(10);int i; do { i = ob1.run(); cout << i << " "; } while (i > CounterNameSpace::lowerbound); cout << endl; CounterNameSpace::counter ob2(20); do { i = ob2.run(); cout << i << " "; } while (i > CounterNameSpace::lowerbound); cout << endl; ob2.reset(100); do { i = ob2.run(); cout << i << " "; } while (i > CounterNameSpace::lowerbound); cout << endl; return 0;}
/**Copyright (c) 2015,烟台大学计算机学院*All rights reserved.*文件名称:test.cpp*作者:吴胜男*完成日期:2015年06月21日*版本号:v1.0**问题描述:阅读下面的程序,写出输出结果(3)请回答:(a)(d)处:为什么可以省去CounterNameSpace::?(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(){ <span style="color:#000099;"> </span><span style="color:#cc33cc;">using CounterNameSpace::upperbound; upperbound = 100; //(a)</span> CounterNameSpace::lowerbound = 0; //(b) CounterNameSpace::counter ob1(10); int i; do { i = ob1.run(); cout << i<<" "; } while( i > CounterNameSpace::lowerbound); cout << endl; <span style="color:#cc33cc;">using namespace CounterNameSpace; counter ob2(20);</span> do { i = ob2.run(); cout << i<<" "; } <span style="color:#ff0000;">while( i > CounterNameSpace::lowerbound);</span> //(c) cout << endl; ob2.reset(100); <span style="color:#cc33cc;">lowerbound = 90;</span> //(d) do { i = ob2.run(); cout << i <<" "; } while( i > lowerbound); return 0;}
/**Copyright (c) 2015,烟台大学计算机学院*All rights reserved.*文件名称:test.cpp*作者:吴胜男*完成日期:2015年06月21日*版本号:v1.0**问题描述:阅读下面的程序,写出输出结果(3)请回答:(a)(d)处:为什么可以省去CounterNameSpace::?(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; <span style="color:#cc33cc;">using namespace CounterNameSpace;</span> counter ob2(20); do { i = ob2.run(); cout << i<<" "; } <span style="color:#ff0000;"> while( i > lowerbound);</span> //(c) cout << endl; ob2.reset(100); lowerbound = 90; //(d) do { i = ob2.run(); cout << i <<" "; } while( i > lowerbound); return 0;}
运行结果:
知识点总结:
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
(a)处:使用了命名空间成员的方法using CounterNameSpace::upperbound;在其后的upperbound就等价于CounterNameSpace::upperbound。
(d)处:是因为使用了using namespace CounterNameSpace;声明了在本作用域中要用到命名空间CounterNameSpace中的成员,在使用该命名空间的任何成员时都不必再用命名空间的限定。
(b)(c)处:是否可以省去CounterNameSpace::?
(b)处不可以。(c)处可以,理由同上。
学习心得:一点一点,扎扎实实的感觉真好。
0 0
- 第十六周 阅读程序一(3)
- 第十六周阅读程序(3)
- 第十六周 程序阅读(3)
- 第十六周项目 阅读程序(3)
- 第十六周程序阅读(3)
- 第十六周阅读程序(3)
- 第十六周阅读程序3(1)
- 第十六周阅读程序4(3)
- 第十六周阅读程序3
- 第十六周 阅读程序3
- 第十六周 程序阅读(补充阅读)
- 第十六周 阅读程序
- 第十六周阅读程序
- 第十六周阅读程序
- 第十六周 阅读程序
- 第十六周 程序阅读
- 第十六周 程序阅读
- 第十六周--阅读程序
- 九度oj 题目1099:后缀子串排序
- 面试题-javascript-面向对象编程
- 在单个页面上组合HTML和PHP代码
- 二叉树的四种遍历
- 小马哥---高仿苹果6 plus A192主板 6592芯片刷机拆机主板图与开机界面图 指纹识别精仿机
- 第十六周 程序阅读(3)
- SVN小试牛刀
- getDrawingCache方法获取ImageView中的图像需要注意的问题
- 关于Android AlertDialog的setView
- 第十三周项目1-动物这样叫(1)
- 学习笔记《C#入门经典(第6版)》C#语言:第3章
- 数据结构——直接插入排序(c++)
- 【Codeforces61E】Enemy is weak
- 第1章 概述