書面作業(yè)_4.doc_第1頁
書面作業(yè)_4.doc_第2頁
書面作業(yè)_4.doc_第3頁
書面作業(yè)_4.doc_第4頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、書面作業(yè)_4.doc 書面作業(yè)_4 1 、 簡答題: (1) 構(gòu)造函數(shù)的作用是什么? 它有哪些特點? 作用是對對象進行初始化 函數(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ù)的功能是什么? 它有哪些特點? 在對象消亡時,自動完成清除工作。 函數(shù)名必須與其類名相同,但該函數(shù)前面加。沒有參數(shù),也沒有返回值,而且不能重載,在一個類中只能有一個析構(gòu)函數(shù)。當撤銷對象時,編譯器會自動調(diào)用析構(gòu)函數(shù)。 (3) 拷貝構(gòu)造函數(shù)有什么作用? 在哪些情況下會調(diào)用拷貝構(gòu)造函數(shù)? 用一個已有的

2、對象來初始化一個被創(chuàng)建的同類對象,是一種特殊的成員函數(shù)。 用類的一個對象去初始化另一個對象;用對象作為函數(shù)實參傳遞給形參時,調(diào)用拷貝構(gòu)造函數(shù);如果函數(shù)的返回值是類的對象,函數(shù)調(diào)用返回時,調(diào)用拷貝構(gòu)造函數(shù)。 (4) 類型轉(zhuǎn)換構(gòu)造函數(shù)有什么作用? 它有什么特點? 實現(xiàn)類型的自動轉(zhuǎn)換; 只有一個參數(shù) 不是復(fù)制構(gòu)造函數(shù) 編譯系統(tǒng)會自動調(diào)用類型轉(zhuǎn)換構(gòu)造函數(shù),建立一個臨時對象 / 臨時變量 (5) 在用 new、delete 運算符創(chuàng)建、刪除對象時,是否會調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)? 用 new 運算符動態(tài)創(chuàng)建對象時也會自動調(diào)用構(gòu)造函數(shù); 用 delete 運算符刪除對象時也會自動調(diào)用析構(gòu)函數(shù) 。 2 、 教

3、材 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 第 第 7 題 #include iostr

4、eam 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; sample d; d = a; cout

5、 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,姓名修改為"李華',最后把這兩個對象的信息輸出到屏幕上。 (提示:姓名是字符指針類型,需要動態(tài)分配

6、、釋放內(nèi)存空間,分別在構(gòu)造函數(shù)、析構(gòu)函數(shù)中實現(xiàn),還要在拷貝構(gòu)造函數(shù)中分配新內(nèi)存空間,以達到深拷貝目的。請參考 程序代碼 4.doc (5) 改進版本) #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 = num; int len = this-

7、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 = teacher.age; virtu

8、alteacher() 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_copy(this-name, teac

9、); 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); this-name = new c

10、harlen + 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 m

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論