freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

c應(yīng)用與開發(fā)案例教程(下)ppt-在線瀏覽

2024-12-04 00:24本頁面
  

【正文】 lic: Person(char *theName,int theOld,unsigned long theId,char theSex)。 Person::Person(char *theName,int theOld,unsigned long theId,char theSex) { if((Name== )) //有效性檢查 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 throw Invalid Person39。 if((Old0)||(Old200)) //有效性檢查 throw Old。M39。amp。m39。amp。F39。amp。f39。 } void fun() { try { coutfun line1endl。M39。 //類 Person 的對象 p1 coutfun line2endl。F39。 //類 Person 的對象 p2 coutfun line3endl。M39。 //類 Person 的對象 p3 coutfun line4endl。F39。 //類 Person 的對象 p4 } catch(char) //捕捉錯誤并進(jìn)行處理 { coutInvalid Sex!endl。 } catch(char *Msg) //捕捉錯誤并進(jìn)行處理 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 { coutInvalid Name!endl。 fun()。 coutin the main function!endl。 【 例 86】 對 【 例 85】 稍加改動 , 使異常的拋出和捕捉在不同函數(shù)中進(jìn)行 。 include include class Person { protected: char Name[20]。 //年齡 unsigned long ID。 //性別 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 public: Person(char *theName,int theOld,unsigned long theId,char theSex)。 Person::Person(char *theName,int theOld,unsigned long theId,char theSex) { if((Name== )) //有效性檢查 throw Invalid Person39。 if((Old0)||(Old200)) //有效性檢查 throw Old。M39。amp。m39。amp。F39。amp。f39。 } void fun() { coutfun line1endl。M39。 //類 Person 的對象p1 coutfun line2endl。F39。 //類 Person 的對象 p2 coutfun line3endl。M39。 //類 Person 的對象 p3 coutfun line4endl。F39。 //類 Person 的對象 p4 } 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 void main() { try { cout”before fun…… ”endl。 cout”end fun…… ”endl。 } catch(int) //捕捉錯誤并進(jìn)行處理 { 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 coutInvalid Old!endl。 } coutendl。 } 下面這個例子演示了如何用異常處理糾正程序運(yùn)行期錯誤 。 include include class String 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 { private: char *str。 } String(char *inString) { str = new char[strlen(inString)+1]。 } void replace(char search, char repl) 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 { int counter。 str[counter] != 39。 counter++) { if(str[counter] == search) { str[counter] = repl。 } 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 }。 //該對象不包含任何東西 (39。,39。)。 } 上面的代碼產(chǎn)生下面的運(yùn)行期錯誤: Segmentation fault (core dumped) 分析:要想糾正如上程序運(yùn)行期錯誤 , 首先要找到出現(xiàn)異常的原因 , 然后采用異常處理機(jī)制去處理異常 。 1. 標(biāo)識異常的原因 根據(jù)分析知道 , 在 main( )函數(shù)中 , String類的一個空對象被 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 創(chuàng)建 。 2. 標(biāo)識異常處理的機(jī)制 采用 try、 throw 、 catch語句進(jìn)行處理 。 int check() { if (str == NULL) 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 return 0。 } public: String() { str = 0。 strcpy(str,inString)。 } int counter。 str[counter] != 39。 counter++) { if(str[counter] == search) { str[counter] = repl。 } }。 //對象不能包含任何的 try try { (39。,39。)。 } 《 C++教程 》 清華大學(xué)出版社 Try、 catch和 throw語句 catch(char *message) { cout Exception : message endl。 下面程序簡單說明 C++標(biāo)準(zhǔn)異常類的使用 。 《 C++教程 》 清華大學(xué)出版社 標(biāo)準(zhǔn) C++庫中的異常類 includeiostream includeexception using namespace std。 //聲明一個 C++標(biāo)準(zhǔn)異常類的對象 throw(theError)。theError) //捕捉 C++標(biāo)準(zhǔn)異常類的對象 { cout( )endl。 //聲明一個 C++標(biāo)準(zhǔn)異常類 logic_error的對象 throw (theLogicError)。theError) //捕捉 C++標(biāo)準(zhǔn)異常類的對象 { cout( )endl。 程序如下: // include class Exception { public: Exception() { } virtual ~Exception() 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? { } virtual void PrintError()=0。 class OutOfMemory:public Exception { public: OutOfMemory() { } ~OutOfMemory() { } 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? virtual void PrintError()。 void OutOfMemory::PrintError() { coutOut of Memory!!endl。 } 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? ~RangError() { } virtual void PrintError()。 } virtual void SetNumber(unsigned long number) { BadNum=number。 }。 } void fn1()。 void fn3(unsigned int *)。 } catch (Exceptionamp。 } } unsigned int *fn2() { unsigned int *n=new unsigned int。 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? return n。 fn3(p)。 delete p。 coutEnter an integer(01000):。 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? if(Number1000 || Number0) throw RangError(Number)。 } 程序的輸出結(jié)果為: Enter an integer(01000): 78 The number is: 78 Enter an integer(01000): 1500 Number out of range. You used 1500! 下面給出一個接近實(shí)用的例子 , 假設(shè)開發(fā)一個人員管理系統(tǒng) ,并且聲明了人這個基本類 Person, 它包含姓名 、 性別 、 年齡 、 證件號碼等數(shù)據(jù) , 我們可以事先估計(jì)一下可能出現(xiàn)的異常情況 , 如用戶忘記輸入姓名 , 年齡不在正常范圍內(nèi) 、 證件號碼不符合要求 、性別不符合要求等等 , 這要求有一個可以報告這類錯誤 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? 的類 InvalidPerson。 看下面的例子并體會異常處理的用法: 【 例 810】 人員管理系統(tǒng)中常用的異常處理 。 class Person。 out)const。 //錯誤原因 const string where。 endif // include include 《 C++教程 》 清華大學(xué)出版社 程序?qū)嵗? include class InvalidPerson:public Error // 從類 Error 派生類InvalidPerson { public: InvalidPerson(const Person *thePerson,const char *theWhy): Error(Person::InvalidCheck,theWhy),pPerson(thePerson) {} //InvalidPerson類構(gòu)造函數(shù) virtual void display(ostreamamp。 //派生類中的出錯內(nèi)容顯示 protected: const Person *pPerson。 class IOError:public Error //從類 Error派生類 IOError 《 C++教程 》
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1