第15周-阅读项目1-异常处理&&命名空间
2015-06-17 20:31
369 查看
问题及代码:
运行结果:
问题及代码:
运行结果:
问题及代码:
运行结果:
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
因为之前已经对相关命名作了声明。
(b)(c)处:是否可以省去CounterNameSpace::?
可以。
知识点总结:
异常处理&&命名空间。
学习心得:
异常处理真是比以前我们的错误提醒高大上好多哦哈哈O(∩_∩)O哈哈~
新技能get√
#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]; }
运行结果:
问题及代码:
#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; }
运行结果:
问题及代码:
#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::?
因为之前已经对相关命名作了声明。
(b)(c)处:是否可以省去CounterNameSpace::?
可以。
知识点总结:
异常处理&&命名空间。
学习心得:
异常处理真是比以前我们的错误提醒高大上好多哦哈哈O(∩_∩)O哈哈~
新技能get√
相关文章推荐
- 正则表达式
- IOS笔记050-事件处理
- 扩展C#与元编程(二)
- 提交游戏到AppStore被打回
- 删除 ComponentFamilies 下的子键和 f256! 值
- C#操作Excel文件(读取Excel,写入Excel)
- SHFileOperation实现拷贝进度条
- 软件测试演义——中高级系列(序)
- socket
- 多线程03-NSOperationQueue(掌握)
- BS CS的区别
- java try catch finally中的return问题
- scp协议原理
- 正则表达式
- 初识JAVA
- 团队开发------第一次冲刺第2天
- Container with most water
- 生产者-消费者问题
- JDK中的Date类的小误区和获取自定义日期的号数和毫秒转日期
- js--小结③