c++面向?qū)ο笳n后答案第11章_第1頁
c++面向?qū)ο笳n后答案第11章_第2頁
c++面向?qū)ο笳n后答案第11章_第3頁
c++面向?qū)ο笳n后答案第11章_第4頁
c++面向?qū)ο笳n后答案第11章_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)1. 概念填空題1.1C+程序?qū)⒖赡馨l(fā)生異常的程序塊放在 try 中,緊跟其后可放置若干個對應的 catch ,在前面所說的塊中或塊所調(diào)用的函數(shù)中應該有對應的 throw ,由它在不正常時拋出 異常 ,如與某一條 catch 類型相匹配,則執(zhí)行該語句。該語句執(zhí)行完之后,如未退出程序,則執(zhí)行 catch后續(xù)語句 。如沒有匹配的語句,則交給C+標準庫中的 termanite 處理。1.2 throw表達式的行為有些像函數(shù)的 函數(shù)調(diào)用 ,而catch子句則有些像函數(shù)的 函數(shù)定

2、義 。函數(shù)的調(diào)用和異常處理的主要區(qū)別在于:建立函數(shù)調(diào)用所需的信息在 編譯 時已經(jīng)獲得,而異常處理機制要求 運行 時的支撐。對于函數(shù),編譯器知道在哪個調(diào)用點上函數(shù)被真正調(diào)用;而對于異常處理,異常是 隨機 發(fā)生的,并沿調(diào)用鏈逆向 查找異常處理子句,這與 運行時的 多態(tài)是 不一樣的 。2.簡答題2.1 C+中的異常處理機制意義,作用是什么?2.2 當在try塊中拋出異常后,程序最后是否回到try塊中繼續(xù)執(zhí)行后面的語句?2.3 什么叫拋出異常? catch可以獲取什么異常參數(shù)?是根據(jù)異常參數(shù)的類型還是根據(jù)參數(shù)的值處理異常?請編寫測試程序驗證。2.4 為什么C+要求資源的取得放在構(gòu)造函數(shù)中,而資源的釋放

3、在析構(gòu)函數(shù)中?3選擇題3.l 下列關(guān)于異常的敘述錯誤的是(A)。A編譯錯屬于異常,可以拋出B運行錯屬于異常C硬件故障也可當異常拋出D只要是編程者認為是異常的都可當異常拋出3.2下列敘述錯誤的是( )。Athrow語句須書寫在時語句塊中Bthrow語句必須在try語句塊中直接運行或通過調(diào)用函數(shù)運行C一個程序中可以有try語句而沒有throw語句Dthrow語句拋出的異常可以不被捕獲3.3 關(guān)于函數(shù)聲明 float fun(int a,int b)throw,下列敘述正確的是( )。A表明函數(shù)拋出float類型異常B表明函數(shù)拋出任何類型異常C表明函數(shù)不拋出任何類型異常D表明函數(shù)實際拋出的異常3.4

4、下列敘述錯誤的是()。Acatch()語句可捕獲所有類型的異常B一個try語句可以有多個catch語句Ccatch()語句可以放在catch語句組的中間D程序中try語句與catch語句是一個整體,缺一不可3.5下列程序運行結(jié)果為(A)。#includeusing namespace std;class Spublic:S( )cout”S”t”;char fun0() S s1;throw(T);return 0;void main()trycoutfun0()”t”;catch(char c)coutc”t”;AS TBO S TCO TDT4.寫出程序運行結(jié)果4.1#include us

5、ing namespace std;int a10=1,2, 3, 4, 5, 6, 7, 8, 9, 10;int fun( int i);void main()int i ,s=0;for( i=0;i=10;i+) try s=s+fun(i);catch(int)cout”數(shù)組下標越界!”endl;couts=”s=10)throw i;return ai;數(shù)組下標越界!S=554.2 #include using namespace std;void f();class Tpublic:T( )coutconstructorendl;trythrow exception;catch(

6、 char*)coutexception”endl;throw exception;T( ) coutdestructor;void main()coutmain function”endl;try f( ); catch( char *) coutexception2endl;coutmain function”endl;void f( ) T t; main functionconstructorexceptionexception2main function5.程序設計題5.1以String類為例,在String類的構(gòu)造函數(shù)中使用new分配內(nèi)存。如果操作不成功,則用try語句觸發(fā)一個ch

7、ar類型異常,用catch語句捕獲該異常。同時將異常處理機制與其他處理方式對內(nèi)存分配失敗這一異常進行處理對比,體會異常處理機制的優(yōu)點。#include #include using namespace std;class Stringpublic:String(const char*);String(const String&);String();void ShowStr()coutsPtrendl;private:char *sPtr;String:String(const char *s)sPtr=new charstrlen(s)+1;if(sPtr=NULL)throw(Constru

8、ctor abnormal);strcpy(sPtr,s);String:String(const String ©)sPtr=new charstrlen(copy.sPtr)+1;if(sPtr=NULL)throw(Copy constructor abnormal);strcpy(sPtr,copy.sPtr);String:String()delete sPtr;int main()tryString str1(This is C+);String str2(str1);catch(char* c)coutcendl;return 0;5.2在5.1的基礎(chǔ)上,重載數(shù)組下標操作

9、符,使之具有判斷與處理下標越界功能。解法一#include #include using namespace std;class Stringpublic:String(const char*);String(const String&);String();char operator(int);void ShowStr()coutsPtrendl;private:char *sPtr;String:String(const char *s)sPtr=new charstrlen(s)+1;if(sPtr=NULL)throw(Constructor abnormal);strcpy(sPtr,

10、s);String:String(const String ©)sPtr=new charstrlen(copy.sPtr)+1;if(sPtr=NULL)throw(Copy constructor abnormal);strcpy(sPtr,copy.sPtr);String:String()delete sPtr;char String:operator(int subscript)if(subscriptstrlen(sPtr)throw(subscript);return *(sPtr+subscript);int main()tryString str1(This is C

11、+);String str2(str1);coutstr13endl;coutstr218endl;catch(char* c)coutcendl;catch(int i)couti下標越界endl;return 0;解法二#include #include #include using namespace std;class Stringpublic:String(const char*);String(const String&);String();char operator(int);void ShowStr()coutsPtrendl;private:char *sPtr;String

12、:String(const char *s)sPtr=new charstrlen(s)+1;if(sPtr=NULL)throw(Constructor abnormal);strcpy(sPtr,s);String:String(const String ©)sPtr=new charstrlen(copy.sPtr)+1;if(sPtr=NULL)throw(Copy constructor abnormal);strcpy(sPtr,copy.sPtr);String:String()delete sPtr;char String:operator(int subscript)

13、if(subscriptstrlen(sPtr)char* out_of_index=Out of range in index of array;throw(out_of_index);return *(sPtr+subscript);int main()tryString str1(This is C+);String str2(str1);coutstr13endl;coutstr218endl;catch(char* c)coutcendl;catch(out_of_range &excp)coutexcp.what()endl;return -1;return 0;5.3定義一個異常類Cexception,有成員函數(shù)reason(),用來顯示異常的類型。定義一個函數(shù)fun1()觸發(fā)異常,在主函數(shù)try模塊中調(diào)用fun1(),在catch模塊中捕獲異常,觀察程序執(zhí)行流程。#includ

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論