



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、本文格式為word版,下載可任意編輯書面作業(yè)_4.doc 書面作業(yè)_4 1 、 簡答題: (1) 構(gòu)造函數(shù)的作用是什么? 它有哪些特點(diǎn)? 作用是對對象進(jìn)行初始化 函數(shù)名必需與其類名相同,一個類可以有多個構(gòu)造函數(shù)(即構(gòu)造函數(shù)重載),也可以沒有構(gòu)造函數(shù),構(gòu)造函數(shù)可以有參數(shù),也可以沒有參數(shù);在創(chuàng)建對象時,系統(tǒng)會自動調(diào)用構(gòu)造函數(shù) (2) 析構(gòu)函數(shù)的功能是什么? 它有哪些特點(diǎn)? 在對象消亡時,自動完成清除工作。 函數(shù)名必需與其類名相同,但該函數(shù)前面加。沒有參數(shù),也沒有返回值,而且不能重載,在一個類中只能有一個析構(gòu)函數(shù)。當(dāng)撤銷對象時,編譯器會自動調(diào)用析構(gòu)函數(shù)。 (3) 拷貝構(gòu)造函數(shù)有什么作用? 在哪些狀況
2、下會調(diào)用拷貝構(gòu)造函數(shù)? 用一個已有的對象來初始化一個被創(chuàng)建的同類對象,是一種特別的成員函數(shù)。 用類的一個對象去初始化另一個對象;用對象作為函數(shù)實(shí)參傳遞給形參時,調(diào)用拷貝構(gòu)造函數(shù);假如函數(shù)的返回值是類的對象,函數(shù)調(diào)用返回時,調(diào)用拷貝構(gòu)造函數(shù)。 (4) 類型轉(zhuǎn)換構(gòu)造函數(shù)有什么作用? 它有什么特點(diǎn)? 實(shí)現(xiàn)類型的自動轉(zhuǎn)換; 只有一個參數(shù) 不是復(fù)制構(gòu)造函數(shù) 編譯系統(tǒng)會自動調(diào)用類型轉(zhuǎn)換構(gòu)造函數(shù),建立一個臨時對象 / 臨時變量 (5) 在用 new、delete 運(yùn)算符創(chuàng)建、刪除對象時,是否會調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)? 用 new 運(yùn)算符動態(tài)創(chuàng)建對象時也會自動調(diào)用構(gòu)造函數(shù); 用 delete 運(yùn)算符刪除對象時
3、也會自動調(diào)用析構(gòu)函數(shù) 。 2 、 教材 p206 第 第 6 題 #includeiostream using namespace std; class csample int x; public: csample() cout c1 endl; csample(int n) x = n; cout c2,x= n endl; ; int main() csample array12; csample array22 = 6,8 ; csample array32 = 12 ; csample *array4 = new csample3; return 0; 3 、 教材 p206 第 第
4、7 題 #include iostream using namespace std; class sample public: int v; sample() ; sample(int n) :v(n) ; sample(sample x) v = 2 + x.v; ; sample printanddouble(sample o) cout o.v; o.v = 2 * o.v; return o; int main() sample a(5); sample b = a; sample c = printanddouble(b); cout endl; cout c.v endl; sam
5、ple d; d = a; cout d.v; 4 、請按下列要求編寫程序: (1)聲明一個老師類 teacher,該類的私有數(shù)據(jù)成員有:工號(num)、姓名(char *pname)、年齡(age)。請定義相應(yīng)的成員函數(shù)來設(shè)置、讀取這些私有成員,并為該類定義構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、類型轉(zhuǎn)換構(gòu)造函數(shù)(只包含工號一個參數(shù))、析構(gòu)函數(shù); (2)在 main 函數(shù)里先定義 teacher 類的一個對象 wang(201,'王偉',35),然后以它為基礎(chǔ)復(fù)制一個對象 li,再將 li 的工號修改為 202,姓名修改為"李華',最終把這兩個對象的信息輸出到屏幕上。 (提
6、示:姓名是字符指針類型,需要動態(tài)安排、釋放內(nèi)存空間,分別在構(gòu)造函數(shù)、析構(gòu)函數(shù)中實(shí)現(xiàn),還要在拷貝構(gòu)造函數(shù)中安排新內(nèi)存空間,以達(dá)到深拷貝目的。請參考 程序代碼 4.doc (5) 改進(jìn)版本) #includecassert #includeiostream using namespace std; class teacher; class teacher public: teacher(int num, const char* name, int age) assert(num = 0); assert(name); assert(age = 0 age = 200); this-num = nu
7、m; int len = this-str_len(name); this-name = new charlen + 1; str_copy(this-name, name); this-age = age; teacher(const teacher teacher) this-num = teacher.num; assert(); int len = this-str_len(); this-name = new charlen + 1; this-str_copy(this-name, ); this-age =
8、teacher.age; virtualteacher() if (this-name) delete this-name; this-name = null; teacher operator=(const teacher teacher) if (this != teacher) this-num = teacher.num; assert(); if (this-name) delete this-name; int len = this-str_len(); this-name = new charlen + 1; this-str_co
9、py(this-name, ); this-age = teacher.age; return *this; public: void setnum(int num) assert(num = 0); this-num = num; void setage(int age) assert(age = 0 age = 200); this-age = age; void setname(const char* name) assert(name); if (this-name) delete this-name; int len = this-str_len(name);
10、 this-name = new charlen + 1; this-str_copy(this-name, name); int getage()const return this-age; int getnum()const return this-num; const char* getname()const return this-name; void showteacherinfo()const cout 工號: this-num endl; cout 姓名: this-name endl; cout 齡: this-age endl; private: int str_len(const char* src) assert(src); int len = 0; while (*src+len); return len; void str_copy(char* dest, const char* src) assert(src); while (*dest+ = *src+); private: int num; char* name; int age; ; void main() teac
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 呼倫貝爾學(xué)院《英語教學(xué)名家教學(xué)賞析》2023-2024學(xué)年第二學(xué)期期末試卷
- 2017小學(xué)生消防課件
- 一流課程匯報答辯
- 泰山護(hù)理職業(yè)學(xué)院《基礎(chǔ)俄語Ⅲ》2023-2024學(xué)年第一學(xué)期期末試卷
- 甘肅省2025屆數(shù)學(xué)三下期末調(diào)研試題含解析
- 星海音樂學(xué)院《小動物臨床用藥專題》2023-2024學(xué)年第二學(xué)期期末試卷
- 浙江省寧波市北侖區(qū)部分校2024-2025學(xué)年小升初數(shù)學(xué)檢測卷含解析
- 石家莊學(xué)院《建筑審美與評論》2023-2024學(xué)年第二學(xué)期期末試卷
- 梅州市蕉嶺縣2025年數(shù)學(xué)四下期末綜合測試試題含解析
- 西安電子科技大學(xué)長安學(xué)院《藥物合成原理》2023-2024學(xué)年第二學(xué)期期末試卷
- 客戶受電工程竣工檢驗(yàn)意見書(南網(wǎng))
- 多媒體課件制作流程圖
- MT_T 695-1997 煤礦用高倍數(shù)泡沫滅火劑通用技術(shù)條件_(高清版)
- 萬科房地產(chǎn)集團(tuán)公司全套管理制度及流程圖
- 《商業(yè)發(fā)票》word版
- 土地使用權(quán)(住宅用地)市場比較法評估測算表
- DFMEA全解(完整版)
- 《教案封面設(shè)計》word版
- 奧迪A4L汽車驅(qū)動橋的結(jié)構(gòu)設(shè)計畢業(yè)設(shè)計
- (最新整理)世界水利發(fā)展史
- 超市新員工進(jìn)職[新版]ppt課件
評論
0/150
提交評論