離散第7章對象生滅_第1頁
離散第7章對象生滅_第2頁
離散第7章對象生滅_第3頁
離散第7章對象生滅_第4頁
離散第7章對象生滅_第5頁
已閱讀5頁,還剩13頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、例 7.1定義日期類,利用構(gòu)造函數(shù)初始化數(shù)據(jù)成員。程序放在頭文件 date.h 中,如下:#include class DateYear, Month, Day;public:Date( )/重載構(gòu)造函數(shù) 1Year=2003; Month=1; Day=1;Date(y)/重載構(gòu)造函數(shù) 2Year=y; Month=1; Day=1;Date(y,m) /重載構(gòu)造函數(shù)3Year=y; Month=m; Day=1;Date(y,m,d) /重載構(gòu)造函數(shù) 4Year=y; Month=m; Day=d;void ShowDate( )cout Year.Month.Dayendl;主函數(shù)源文件

2、為 Li1005.cpp,內(nèi)容如下:#includedate.hvoid main( )Date d1;Date d2(2008); Date d3(2008, 10);Date d4(2008, 10, 6);d1.ShowDate( ); d2.ShowDate( ); d3.ShowDate( ); d4.ShowDate( );/自動調(diào)用構(gòu)造函數(shù) 1/自動調(diào)用構(gòu)造函數(shù) 2/自動調(diào)用構(gòu)造函數(shù) 3/自動調(diào)用構(gòu)造函數(shù) 4運(yùn)行結(jié)果是:2003.1.12008.1.12008.10.12008.10.6當(dāng)然可以定義帶缺省值的構(gòu)造函數(shù),將上述構(gòu)造函數(shù)簡化,下述程序的功能與上述程序相當(dāng):#inclu

3、de classDateYear, Month, Day;public:Date(y=2003,m=1,d=1)/帶參數(shù)缺省值的構(gòu)造函數(shù)Year=y; Month=m; Day=d;void ShowDate( )cout Year.Month.Dayendl;void main( )Date d1, d2(2008), d3(2008, 10), d4(2008, 10, 6);d1.ShowDate( ); d2.ShowDate( ); d3.ShowDate( ); d4.ShowDate( );運(yùn)行結(jié)果與上例一樣。返回 ppt 講稿例 7.2調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)的時機(jī)#includ

4、e classDateYear, Month, Day;public:Date(y=2002,m=1,d=1)Year=y; Month=m; Day=d; coutConstructor: ; ShowDate( );void ShowDate( )cout Year.Month.Dayendl;Date( )coutDestructor: ; ShowDate( );Date d4(2008, 4, 4);void fun( )/全局對象(靜態(tài)的)cout 進(jìn)入 fun( )函數(shù)!n;sic Date d2(2008, 2, 2); /局部靜態(tài)對象Date d3(2008, 3, 3);/

5、局部動態(tài)對象cout 退出 fun( )函數(shù)!n;void main( )cout 進(jìn)入 main( )函數(shù)!n;Date d1(2008, 1, 1);fun( );/局部動態(tài)對象fun( );cout 退出 main( )函數(shù)!n;此程序運(yùn)行結(jié)果是: Constructor: 2008.4.4進(jìn)入 main( )函數(shù)!Constructor: 2008.1.1進(jìn)入 fun( )函數(shù)!/調(diào)用構(gòu)造函數(shù),產(chǎn)生 d4 對象/ 調(diào)用構(gòu)造函數(shù),產(chǎn)生 d1 對象/ 第 1 次進(jìn)入 fun( )函數(shù),產(chǎn)生下述 d2, d3 對象Constructor: 2008.2.2Constructor: 2008.

6、3.3退出 fun( )函數(shù)!/ 退出 fun( )函數(shù),撤消 d3 對象,不撤消 d2 對象Destructor: 2008.3.3進(jìn)入 fun( )函數(shù)!/ 第 2 次進(jìn)入 fun( )函數(shù),再次產(chǎn)生 d3 對象Constructor: 2008.3.3退出 fun( )函數(shù)!Destructor: 2008.3.3退出 main( )函數(shù)!/ 退出 fun( )函數(shù),撤消 d3 對象/ 退出 main ( )函數(shù),撤消 d1, d2, d4 對象Destructor: 2008.1.1Destructor: 2008.2.2Destructor: 2008.4.4返回 ppt 講稿例 7

7、.4定義一個“平面坐標(biāo)點(diǎn)”類,測試拷貝構(gòu)造函數(shù)的調(diào)用。/頭文件 po class Pox, y; public:.hPo(a=0,b=0)/缺省構(gòu)造函數(shù)x=a; y=b;PoPo(Po( )&p);/拷貝構(gòu)造函數(shù)原型說明/析構(gòu)函數(shù)coutx,y Destructor Called.n ;void Show( )coutPoGetx( ): x,yendl;return x; Gety( ) return y;Po:Po(Po&p) /定義拷貝構(gòu)造函數(shù)x=p.x; y=p.y;coutx,y Copy-initialization ConstructorCalled.n;/文件 Li1009.c

8、pp #include #include po void main( ).hPo Pop1(6, 8), p2(4, 7);p3(p1);/ A 調(diào)用拷貝構(gòu)造函數(shù)Pop4=p2;/ B 調(diào)用拷貝構(gòu)造函數(shù)p1.Show( );p3.Show( );p2.Show( );p4.Show( );此程序運(yùn)行結(jié)果是:6, 8 Copy-initialization Constructor Called.4, 7 Copy-initialization Constructor Called.Po Po Po: 6, 8: 6, 8: 4, 7Po: 4, 74, 7 Destructor Called.6

9、, 8 Destructor Called.4, 7 Destructor Called.6, 8 Destructor Called./撤銷 P4撤銷 P3撤銷 P2撤銷 P1析構(gòu)函數(shù)與構(gòu)造函數(shù)的調(diào)用順序相反返回 ppt 講稿例 7.5不定義拷貝構(gòu)造函數(shù)時,運(yùn)行出錯。/文件 Li1010.cpp #include #include class Studentchar *Name;/,注意:用指針實(shí)現(xiàn)Age;public:Student(char *namep,Age=age;age)/構(gòu)造函數(shù)if(namep) /在構(gòu)造函數(shù)中,需動態(tài)申請空間Name=new charstrlen(namep

10、)+1; strcpy(Name, namep);else NaLL;Student( )/因在構(gòu)造函數(shù)中動態(tài)申請了空間,/則在析構(gòu)函數(shù)中,需if(Name) delete Name;空間void Show( )cout Name , Age endl;void main( )Student a(Ge Student b=a;e, 20);/ A此程序運(yùn)行時出錯,原因是: 沒有定義類的拷貝構(gòu)造函數(shù)。系統(tǒng)自動產(chǎn)生的拷貝構(gòu)造函數(shù)如下:Student:Student(const Student &s)Name = s.Name; Age = s.Age;/ 注意:地址值直接賦值ab正確的做法是,定義

11、如下拷貝構(gòu)造函數(shù):Student:Student(const Student &s)Age=s.Age;iame)Name = new charstrlen(s.Name)+1; /C strcpy(Name, s.Name);else NaLL;abGeorge0Name: 2000Age: 20George0Name: 1000Age: 20Name: 1000Age: 20George0Name: 1000Age: 20返回 ppt 講稿例 7.6文件 po在本例中,使用例 7.4 中“平面坐標(biāo)點(diǎn)”類的頭.h,測試用對象做函數(shù)參數(shù)及函數(shù)返回值時拷貝構(gòu)造函數(shù)的使用。#include po.

12、hp,/普通函數(shù),不是類的成員函數(shù)Pomove(Poxoffset,yoffset)x = p.Getx( )+xoffset, y = p.Gety( )+yoffset;Pot(x, y);return t;void main( )Pop1(6, 8), p2;p2=move(p1, 2, 4);此程序運(yùn)行結(jié)果是:6, 8 Copy-initialization Constructor Called. /APop=p1/參數(shù)傳遞時Po內(nèi)存臨時對象=t。/返回對象時8, 12 Copy-initialization Constructor Called. / B 8, 12 Destruct

13、or Called. / 撤消對象 t6, 8 Destructor Called./ 撤消對象 p8, 12 Destructor Called. / 撤消內(nèi)存臨時對象8, 12 Destructor Called. / 撤消對象 p2 6, 8 Destructor Called./ 撤消對象 p1返回 ppt 講稿例 7.7利用構(gòu)造函數(shù)完成類型轉(zhuǎn)換/文件 Li1013.cpp #include class Complexdouble Real, Image; public:Complex(double x=0, double y=0)Real=x; Image=y; Show( );co

14、ut 調(diào)用了構(gòu)造函數(shù)n;Complex( )Show( );cout 調(diào)用了析構(gòu)函數(shù)n;void Show( )cout(Real,Image);void main( )Complex c1(3, 5), c2;/Ac1 = 8.0;/Bc2 = Complex(9.0, 9.0);/C此程序運(yùn)行結(jié)果是:(3, 5)調(diào)用了構(gòu)造函數(shù)/在 A 行創(chuàng)建 c1 對象時,/調(diào)用構(gòu)造函數(shù)/在 A 行創(chuàng)建 c2 對象時,/調(diào)用構(gòu)造函數(shù)/創(chuàng)建、撤消臨時對象,B 行(0, 0)調(diào)用了構(gòu)造函數(shù)(8, 0)調(diào)用了構(gòu)造函數(shù)(8, 0)調(diào)用了析構(gòu)函數(shù)(9, 9)調(diào)用了構(gòu)造函數(shù)(9, 9)調(diào)用了析構(gòu)函數(shù)/創(chuàng)建、撤消臨時對

15、象,C 行(9, 9)調(diào)用了析構(gòu)函數(shù)/在程序結(jié)束,/撤消 c2 對象時,調(diào)用析構(gòu)函數(shù)(8, 0)調(diào)用了析構(gòu)函數(shù) /在程序結(jié)束,/撤消 c1 對象時,調(diào)用析構(gòu)函數(shù)返回 ppt 講稿例 7.8 初始化對象成員。#include #include class Po/定義“點(diǎn)”類等價于 c1 = Complex(8.0);x, y;public: Po(a=0,b=0)x=a; y=b; coutx,yPon;構(gòu)造Getx( ) return x;Gety( ) return y;Po( ) coutx,yPon;析構(gòu);class Line/定義“線”類,兩點(diǎn)決定一條線width, color; /指

16、定“線”的寬度、顏色Pop1, p2;/指定“線”的兩個端點(diǎn)public:Line(x1,y1,x2,y2,w,c) :p1(x1,y1), p2(x2,y2) / Awidth=w; color=c; coutwidth,color構(gòu)造 Linen;double LineLen( )double len;x1, y1, x2, y2; x1=p1.Getx( ); y1=p1.Gety( );x2=p2.Getx( ); y2=p2.Gety( ); len=sqrt(x1x2)*(x1x2)+(y1y2)*(y1y2); return(len);Line( )coutwidth,color

17、析構(gòu) Linen;void main( )Line Li(0, 0, 1, 1, 3, 6);cout長度=Li.LineLen( )endl;此程序運(yùn)行結(jié)果是:0, 0構(gòu)造 Po1, 1構(gòu)造 Po3, 6 構(gòu)造 Line長度=1.414213, 6 析構(gòu) Line1, 1 析構(gòu) Po0, 0 析構(gòu) Po返回 ppt 講稿例 7.9 定義學(xué)生類,利用構(gòu)造函數(shù)初始化數(shù)據(jù)成員,利用析構(gòu)函數(shù)做工作。#include #include class Studentchar Num10; /學(xué)號,注意:用數(shù)組實(shí)現(xiàn)char *Name;/,注意:用指針實(shí)現(xiàn)Score;/成績public:Student(ch

18、ar *nump, char *namep,score)if(nump) /在構(gòu)造函數(shù)中,/不需要動態(tài)申請 Num 成員的空間strcpy(Num, nump);elsestrcpy(Num, );if(namep) /在構(gòu)造函數(shù)中,/需動態(tài)申請 Name 成員的空間Name=new charstrlen(namep)+1; strcpy(Name, namep);else Name=0;Score=score; coutConstructor Called!n;Student( )/在析構(gòu)函數(shù)中,/需Name 成員的空間if(Name) delete Name;coutDesturctor Called!n;void Show( )cout Num endl; cout Name endl; cout Score endl;void main( )Student a(040120518, Ge a.Show( );此程序運(yùn)行結(jié)果是:e, 80);Constructor Called!/調(diào)用構(gòu)造函數(shù)時的輸出 040120518Gee80Desturctor Called!/調(diào)用析構(gòu)函數(shù)時的輸出返回 ppt 講稿例 7.10 說明 this 指針的使用。#include classSx, y; public:lefun(a,b)this x =

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論