第十六周阅读项目:异常处理和命名空间
来源:互联网 发布:html按钮水波纹效果js 编辑:程序博客网 时间:2024/05/25 23:58
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];}
运行结果:
学习心得:
try块是检查语句,throw用来当出现异常时发出的一个异常信号,而catch用来捕捉异常信息。
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;}
运行结果:
学习心得:
命名空间的作用是建立一些相互分隔的作用域,把一些全局实体分隔开来,以免产生名字冲突。ob2.reset(100);因为100==upperbound,所以i==0,所以直接输出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;}
运行结果:
学习心得:
a,c,d处可以省去CounterNameSpace::,b处不可以省去CounterNameSpace::。因为using CounterNameSpace::upperbound;声明了upperbound为命名空间成员,而lowerbound没有声明,所以不能省略。在用using声明后,在其后程序中出现的upperbound,lowerbound就是隐含的指CounterNameSpace::upperbound,CounterNameSpace::lowerbound,所以c,d处可以省略。
4.代码:
#include <iostream>using namespace std;void f();class T{public: T( ) { cout<<"constructor"<<endl; try { throw "exception"; } catch(char*) { cout<<"exception"<<endl; } throw "exception"; } ~T( ) { cout<<"destructor"; }};int main(){ cout<<"main function"<< endl; try { f( ); } catch(char *) { cout<<"exception2"<<endl; } cout<<"main function"<<endl; return 0;}void f( ){ T t;}
运行结果:
0 0
- 第十六周阅读项目:异常处理和命名空间
- 第十六周阅读程序:异常处理和命名空间
- 第15周-阅读项目1-异常处理&&命名空间
- 第15周-阅读项目2-异常处理&&命名空间
- 第16周-异常处理和命名空间-课后实践·阅读程序
- 第16周《C++语言基础》程序阅读——异常处理和命名空间
- 第16周-异常处理和命名空间-项目1-平方根的异常
- 第16周-异常处理和命名空间-项目2-有些数的阶乘不算了
- 第16周-异常处理和命名空间-项目3-max带来的冲突
- 《C++语言基础》程序阅读——异常处理和命名空间
- 《C++语言基础》实践项目——异常处理和命名空间
- 命名空间与异常处理
- c++异常处理/命名空间
- 第十六周阅读项目-2
- 第十六周阅读项目-3
- 第十六周阅读项目-8
- 第十六周项目三 max带来的冲突(命名空间)
- PHP异常处理以及命名空间
- 关于MyEclipse(jsp+Tomcat)与 SQLSERVER 2012 JDBC连接的问题解决方法
- 身份证验证js
- SVN Unable to connect to a repository at URL---转懒洋洋
- 10分钟掌握Linux vi编辑器常见命令的使用,最简单的vi编辑器教程
- Android开发教程--设置ImageView图片的显示比例
- 第十六周阅读项目:异常处理和命名空间
- 添加openssl/rsa.h file not found 问题解决方法
- myeclipse内存溢出
- cruisecontrol的搭建和使用
- Python Interview Question and Answers
- nodejs+socket聊天系统
- Linux命令查询
- BDC的执行模式与更新模式
- JDK自带VM分析工具jps,jstat,jmap,jconsole