C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt

上传人(卖家):三亚风情 文档编号:2493617 上传时间:2022-04-25 格式:PPT 页数:46 大小:187KB
下载 相关 举报
C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt_第1页
第1页 / 共46页
C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt_第2页
第2页 / 共46页
C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt_第3页
第3页 / 共46页
C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt_第4页
第4页 / 共46页
C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt_第5页
第5页 / 共46页
点击查看更多>>
资源描述

1、EE4E. C+ ProgrammingContentsIntroductionAn object oriented approach to exception handlingclass myClasspublic: int readFile(.)doif (!end_of_file)/ read the fileelsereturn 1; while not_end_of_file return number_of_bytes_read;The try-catch model of exception handlingApplicationObject 1Method callError

2、conditiontry clausecatch clauseException handling example#include #include class DivideByZeroException : public exceptionpublic:DivideByZeroException:DivideByZeroException(): exception(Attempted divide by zero) ;double quotient(int num, int den)if (den=0)throw DivideByZeroException();return (double)

3、(num)/den;int main()int number1,number2;double result;cout number1 number2)try result=quotient(number1,number2);cout The quotient is result endl;catch(DivideByZeroException &dzException)cout Exception! dzException.what() endl;cout nEnter two integers : ;return 0;Re-throwing exceptionsmainObject 2Obj

4、ect 1 throws exceptionCalls method ofCalls method ofre-throwhandles exceptionre-throwclass MyClass1 public:MyClass1() void aMethod1()trythrow exception();catch (exception &caughtException)cout Exception thrown in aMethod1 endl;throw;class MyClass2 public:MyClass2() void aMethod2()MyClass1 myObject1;

5、trymyObject1.aMethod1();catch(exception &caughtException)cout Exception re-thrown in aMethod2 endl;throw;int main()MyClass2 myObject2;trymyObject2.aMethod2();catch(exception &caughtException)cout Exception handled in main endl;cout “Program terminates ;return 0;Exception thrown in aMethod1Exception

6、re-thrown in aMethod2Exception handled in mainProgram terminatesException specificationsint aFunction(int arg)throw (ExceptionA, ExceptionB, ExceptionC) / function body)int aFunction(int arg) / function body)int aFunction(int arg)throw() / function body)int aFunction(int arg) throws IOException / Ja

7、va / function body)Multi-threading in C+A simple C+ Thread classclass Thread private:string m_strName;public:Thread();Thread(const char* nm);virtual Thread();void setName(const char* nm);string getName() const;void start();virtual void run();void sleep(long ms);void suspend();void resume();void stop

8、();void setPriority(int p);bool wait(const char* m,long ms=5000);void release(const char* m);/ Thread prioritiesstatic const int P_HIGHEST;static const int P_LOWEST;static const int P_NORMAL;Creating a simple multi-threaded application#include #include using namespace std;#include ou_thread.husing n

9、amespace openutils;class MyThread : public Threadprivate:int m_nCount;public:MyThread(int n,const char* nm)Thread:setName(nm);m_nCount=n;void run()for (int i=0; im_nCount; i+)cout getName().c_str() : i start();t2-start();t1-stop();t2-stop();catch(ThreadException ex)cout ex.getMessage().c_str();delet

10、e t1;delete t2;return 0;Thread synchronizationUnsynchronised threadsThread 1Thread 2Update databaseRead databasePre-emptPre-emptSynchronised threadsThread 1Thread 2Update databaseRead databasevoid MyThread:run()wait(MyMutex);for (int i=0; im_nCount; i+)cout getName().c_str() : i start();t2-start();t1-stop();t2-stop();m.release();catch(ThreadException ex)cout start();t1-setPriority(Thread:P_LOWEST);t2-start();t2-setPriority(Thread:P_HIGHEST);t1-stop();t2-stop();catch(ThreadException ex)cout ex.getMessage().c_str();delete t1;delete t2;return 0;And finally.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(C++ProgrammingLecture6AdvancedTopics[C++编程讲座6高级主题(PPT-46)课件.ppt)为本站会员(三亚风情)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|