



版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、書面作業_4.doc 書面作業_4 1 、 簡答題: (1) 構造函數的作用是什么? 它有哪些特點? 作用是對對象進行初始化 函數名必須與其類名相同,一個類可以有多個構造函數(即構造函數重載),也可以沒有構造函數,構造函數可以有參數,也可以沒有參數;在創建對象時,系統會自動調用構造函數 (2) 析構函數的功能是什么? 它有哪些特點? 在對象消亡時,自動完成清除工作。 函數名必須與其類名相同,但該函數前面加。沒有參數,也沒有返回值,而且不能重載,在一個類中只能有一個析構函數。當撤銷對象時,編譯器會自動調用析構函數。 (3) 拷貝構造函數有什么作用? 在哪些情況下會調用拷貝構造函數? 用一個已有的
2、對象來初始化一個被創建的同類對象,是一種特殊的成員函數。 用類的一個對象去初始化另一個對象;用對象作為函數實參傳遞給形參時,調用拷貝構造函數;如果函數的返回值是類的對象,函數調用返回時,調用拷貝構造函數。 (4) 類型轉換構造函數有什么作用? 它有什么特點? 實現類型的自動轉換; 只有一個參數 不是復制構造函數 編譯系統會自動調用類型轉換構造函數,建立一個臨時對象 / 臨時變量 (5) 在用 new、delete 運算符創建、刪除對象時,是否會調用構造函數和析構函數? 用 new 運算符動態創建對象時也會自動調用構造函數; 用 delete 運算符刪除對象時也會自動調用析構函數 。 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,該類的私有數據成員有:工號(num)、姓名(char *pname)、年齡(age)。請定義相應的成員函數來設置、讀取這些私有成員,并為該類定義構造函數、拷貝構造函數、類型轉換構造函數(只包含工號一個參數)、析構函數; (2)在 main 函數里先定義 teacher 類的一個對象 wang(201,'王偉',35),然后以它為基礎復制一個對象 li,再將 li 的工號修改為 202,姓名修改為"李華',最后把這兩個對象的信息輸出到屏幕上。 (提示:姓名是字符指針類型,需要動態分配
6、、釋放內存空間,分別在構造函數、析構函數中實現,還要在拷貝構造函數中分配新內存空間,以達到深拷貝目的。請參考 程序代碼 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高校教師提升學業評價素養的策略及實施路徑
- 2025年移動應用程序開發與設計考試試題及答案
- 2025年文化產業管理專業畢業生考試試題及答案
- 2025年社會政策與社會保障專業知識測試卷及答案
- 2025年金融分析師職業資格考試試卷及答案
- 2025年計算機網絡安全考試試題及答案
- 2025年供應鏈金融與風險控制的綜合能力考試題及答案
- 特價餐具倉庫管理制度
- 特殊手術麻醉管理制度
- 特殊環節瓦斯管理制度
- 臨商銀行股份有限公司招聘筆試真題2024
- 近代史第三章試題及答案
- 地理●甘肅卷丨2024年甘肅省普通高中學業水平等級性考試高考地理真題試卷及答案
- 吊裝-運輸方案(3篇)
- 【8道期末】安徽省蕪湖市無為市2023-2024學年八年級下學期期末道德與法治試題(含解析)
- 2025年鋼絲材料項目市場調查研究報告
- 敘事護理分享課件
- 2025國家開放大學《員工勞動關系管理》形考任務1234答案
- 2025年湖北省新高考信息卷(一)物理試題及答案
- 部編版小學語文《十六年前的回憶》評課稿
- 礦機托管經營合同協議
評論
0/150
提交評論