




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、怎樣使用類和對象對象的初始化不能在聲明類時初始化類只是抽象的類型,不占用存儲空間如果一個類的數據成員都是公用的,則可以在定義對象時,對它們初始化。利用構造函數初始化構造函數與類名同名,沒有任何類型,也沒有返回值在建立對象時,自動執行構造函數。如果用戶沒有定義構造函數,則系統自動生成一個空的、無參的構造函數。 #include using namespace std;class Timeprivate:int hour, minute, sec;public:Time( )hour=12; minute=0; sec=0;void set_time( );void show_time( ) co
2、nst;int main( )Time t1;t1.show_time( );t1.set_time( );t1.show_time( );return 0;void Time:set_time( )cinhourminutesec;void Time:show_time( ) constcouthour:minute;cout:secendl;P256 例9.1#include using namespace std;class Boxpublic:Box(int,int,int);int volume( ) const;private:int height, width, length;B
3、ox:Box(int h, int w, int len) height=h; width=w; length=len;int Box:volume( ) const return height*width*length;int main( )Box b1(12, 25,30);coutThe volume of the 1st box is b1.volume( )endl;Box b2(15, 30, 21);coutThe volume of the 2nd box is b2.volume( )endl;return 0;P259 例9.2對象的初始化(2)可以采用參數初始化表來實現數
4、據成員的初始化形式:類名:構造函數名(參數表):成員初始化表 構造函數體構造函數的重載:一個類中可以定義多個構造函數,它們名字相同,而參數個數或類型不同。構造函數的參數也可以指定默認值。#include using namespace std;class Boxpublic:Box(int h,int w,int l):height(h), width(w), length(l)int volume( ) const;private:int height, width, length;int Box:volume( ) const return height*width*length;int
5、main( )Box b1(12, 25,30);coutThe volume of the 1st box is b1.volume()endl;Box b2(15, 30, 21);coutThe volume of the 2nd box is b2.volume()endl;return 0;#include using namespace std;class Boxpublic:Box(int h,int w,int l):height(h), width(w), length(l)Box( )height=width=length=10;int volume( ) const;pr
6、ivate:int height, width, length;int Box:volume( ) constreturn height*width*length;int main( )Box b1;coutThe volume of the 1st box is b1.volume( )endl;Box b2(15, 30, 21);coutThe volume of the 2nd box is b2.volume( )endl;return 0;#include using namespace std;class Boxpublic:Box(int h=10, int w=10, int
7、 l=10):height(h), width(w), length(l)int volume( ) const;private:int height, width, length;int Box:volume( ) const return height*width*length;int main( )Box b1;coutThe volume of the 1st box is b1.volume( )endl;Box b2(15);coutThe volume of the 2nd box is b2.volume( )endl;return 0;析構函數在對象的生命期結束時,會自動執行
8、析構函數。函數中定義的對象,在函數調用結束時析構用new動態建立的對象,用delete釋放時析構靜態局部變量或全局對象在main函數結束或調用exit函數時析構函數名為類名前加“”。#include #include using namespace std;class Studentpublic: Student(int n, string na):num(n),name(na) coutnum constructedendl; Student( ) coutnum destructedendl;private:int num; string name;int main( )Student s
9、1(10010, Wang li);cout*s1*endl;Student s2(10011, Zhang fang);cout*s2*endl;return 0;P266 例9.5同時需要析構的多個對象,先構造的后析構對象數組和對象指針對象數組:由多個同類的對象構成的數組。對象指針對象空間的起始地址是對象的指針定義指向類對象的指針變量: 類名 *對象指針名;this指針:指向本類對象的指針,值是當前被調用的成員函數所在的對象的起始地址。指向對象成員地址的指針變量是對象成員的指針定義形式:數據類型名 *指針變量名;指向對象成員函數的指針#include using namespace std
10、;class Boxpublic: Box(int h=10, int w=12, int l=15):height(h), width(w), length(l) int volume( ) const;private: int height, width, length;int Box:volume( ) const return height*width*length; int main( ) Box a3=Box( ), Box(20), Box(20,30,40); for(int i=0; i3; i+) coutvolume of ai is ai.volume( )endl;
11、return 0;對象數組 P270 例9.6注意初始化#include using namespace std;class Boxpublic: Box(int h=10, int w=12, int l=15):height(h), width(w), length(l) int volume( ) const;private: int height, width, length;int Box:volume( ) const return height*width*length;int main( ) Box a3=Box( ),Box(20), Box(20,30,40); Box *
12、p=a; for(int i=0; i3; i+) coutvolume of ai is volume( )endl; p+; return 0;對象指針#include using namespace std;class Studentpublic:int num;float score;void max(const Student *p, int count) int id=0; for(int i=1; i(*(p+id).score) id=i; coutnum=(*(p+id).numendl;int main( ) const int count=5; Student stcou
13、nt; for(int i=0; isti.numsti.score; max(st, count); return 0;對象指針P306 練習5#include using namespace std;class Timepublic: Time(int h, int m, int s):hour(h),minute(m),sec(s) void get_time( ) couthour:minute:secendl; int hour, minute, sec;int main( ) Time t1(10,13,56); int *p1=&t1.hour; cout*p1get_time( ); void(Time:*p3)( ); p3 = &Time:get_time; (t1.*p3)( ); return 0;成員函數指針P273 例9.7#include using namespace std;class Boxpublic: Box(int h=10, int w=12, int l=15):height(h), width(w), length(l)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年超高壓電纜輸電系統合作協議書
- 2025-2030中國有機燕麥行業市場發展趨勢與前景展望戰略研究報告
- 暖通工程專業分包合同
- 2025-2030中國工程咨詢行業市場發展前瞻及投資戰略研究報告
- 2025-2030中國實木床架行業發展趨勢及發展前景研究報告
- 2025-2030中國大件運輸行業市場競爭格局及發展趨勢與投資前景研究報告
- 固體廢物處理技術研發合同
- 新能源汽車銷售合同
- 物資設備采購及質量保證協議
- 2025年醫保知識考試題庫及答案:醫保患者權益保障政策實施真題解析
- 維生素K2行業研究、市場現狀及未來發展趨勢(2020-2026)
- 定遠縣蔡橋水庫在建工程實施方案
- 繪本故事《三只小豬蓋房子》課件
- GB 13296-2013 鍋爐、熱交換器用不銹鋼無縫鋼管(高清版)
- 部編版八年級語文下冊寫作《學寫讀后感》精美課件
- LED顯示屏項目立項報告(模板參考)
- 最新版電梯日常維護保養記錄
- 發燒的地球ppt課件
- 區間盾構始發關鍵節點評估報告
- 建筑構造上冊——門和窗一PPT課件
- 20t·35mU型雙梁門式起重機金屬結構設計
評論
0/150
提交評論