15第十六周实践项目——程序阅读
2015-06-21 16:52
453 查看
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 6 月 21 日
* 版 本 号:v1.0
*
* 问题描述:
* 输入描述:
* 程序输出:
*/
(1)
当循环超出时:
(2)
(3)
问:
①为什么可以省去CounterNameSpace::?
②是否可以省去CounterNameSpace::?
答:
①因为在前面已经做出了相关的声明using namespace CounterNameSpace;
②可以
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 6 月 21 日
* 版 本 号:v1.0
*
* 问题描述:
* 输入描述:
* 程序输出:
*/
(1)
#include <iostream> using namespace std; int a[10]={1,2,3,4,5,6,7,8,9,10}; int fun(int i); int main() { int i,s=0; for (i=0;i<=10;i++) { try { s=s+fun(i); } catch(int) { cout<<"数组下标越界!"<<endl; } } cout<<"s="<<s<<endl; return 0; } int fun(int i) { if (i>10) throw i; return a[i]; }
当循环超出时:
(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; }
(3)
#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; }
问:
①为什么可以省去CounterNameSpace::?
②是否可以省去CounterNameSpace::?
答:
①因为在前面已经做出了相关的声明using namespace CounterNameSpace;
②可以
相关文章推荐
- 第十六周阅读程序(2)
- fastjson 使用方法
- 黑马程序员——集合(三)
- Linux内核(二)中断基本概念
- 黑马程序员---API集合
- 重新出发
- JVM原理、架构—JVM运行区域
- Uva - 11572 - Unique Snowflakes
- Error:No suitable device found: no device found for connection "System eth0"
- Uva - 11572 - Unique Snowflakes
- android开发网络请求的几种方法
- 一步步走进Android MaterialDesign 之 其余小控件
- 程序员技术练级攻略(皓哥)
- 练习代码(四)多态
- lvs之dr技术的学习与实践
- JAVA 企业级框架、安全权限、高并发、多线程、稳定强
- jsp——学习篇:jsp文件的构成
- lvs之nat技术的学习与实践
- 调试内存泄漏问题的一些经验
- 不要在析构函数中抛出异常