類組合成員初始化.ppt_第1頁
類組合成員初始化.ppt_第2頁
類組合成員初始化.ppt_第3頁
類組合成員初始化.ppt_第4頁
類組合成員初始化.ppt_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1,C+ 中,提供了用一個對象值創(chuàng)建并初始化另一個對象的方法,完成該功能的是拷貝構(gòu)造函數(shù)。例如: Tdate d1(2002,3,1); Tdate d2(d1); 用構(gòu)造函數(shù)創(chuàng)建d1 的值初始化新創(chuàng)建的對象d2。 拷貝構(gòu)造函數(shù)的特點: 1、拷貝構(gòu)造函數(shù)名字與類同名,沒有返回類型; 2、拷貝構(gòu)造函數(shù)只有一個形參數(shù),該參數(shù)是該類的對象的引用;,拷貝構(gòu)造函數(shù)(Copy Constructor),拷貝構(gòu) 造 函 數(shù),2,3、拷貝構(gòu)造函數(shù)的格式如下: :( /拷貝構(gòu)造函數(shù),拷貝構(gòu) 造 函 數(shù),3,int xcoord() return x; int ycoord() return y; private

2、: int x,y; ; Tpoint:Tpoint(Tpoint ,拷貝構(gòu) 造 函 數(shù),4,void main() Tpoint p1(5,7); Tpoint p2(p1); cout“p2=”p2.xcoord()“,”p2.ycoord()endl; ,結(jié)果: Copy_initialization constructor called. P2=5,7,拷貝構(gòu) 造 函 數(shù),5,注意: 如果一個類中沒有定義拷貝構(gòu)造函數(shù),則系統(tǒng)自動生成一個缺省拷貝構(gòu)造函數(shù),其功能是將已知對象的所有數(shù)據(jù)成員的值拷貝給對應(yīng)對象的數(shù)據(jù)成員。,class A float x, y; public: A(float

3、 a, float b) x=a; y=b; cout調(diào)用了構(gòu)造函數(shù)n; void Print() coutxtyendl; ;,拷貝構(gòu) 造 函 數(shù),6,調(diào)用了構(gòu)造函數(shù) 1.02.0 1.02.0 1.02.0,void main(void) A a1(1.0, 2.0); A a2(a1); A a3 = a1; /可以這樣賦值 a1.Print(); a2.Print(); a3.Print();,拷貝構(gòu) 造 函 數(shù),7,拷貝構(gòu)造函數(shù)舉例,class Point public: Point(int xx=0,int yy=0)X=xx; Y=yy; Point(Point ,拷貝構(gòu) 造 函

4、 數(shù),8,拷貝構(gòu)造函數(shù)調(diào)用,(1)當用類的一個對象去初始化該類的另一個對象時系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)實現(xiàn)拷貝賦值。 void main(void) Point A(1,2); Point B(A); /拷貝構(gòu)造函數(shù)被調(diào)用 coutB.GetX()endl; ,拷貝構(gòu) 造 函 數(shù),9,(2)若函數(shù)的形參為類對象,調(diào)用函數(shù)時,實參賦值給形參,系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)。例如: void fun1(Point p) coutp.GetX()endl; void main() Point A(1,2); fun1(A); /調(diào)用拷貝構(gòu)造函數(shù) ,拷貝構(gòu) 造 函 數(shù),10,(3)當函數(shù)的返回值是類對象時,系

5、統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)。例如: Point fun2() Point A(1,2); return A; /調(diào)用拷貝構(gòu)造函數(shù) void main() Point B; B=fun2(); ,拷貝構(gòu) 造 函 數(shù),11,注意:,如果程序員沒有為類聲明拷貝初始化構(gòu)造函數(shù),則編譯器自己生成一個拷貝構(gòu)造函數(shù)。 這個構(gòu)造函數(shù)執(zhí)行的功能是:用作為初始值的對象的每個數(shù)據(jù)成員的值,初始化將要建立的對象的對應(yīng)數(shù)據(jù)成員。,拷貝構(gòu) 造 函 數(shù),12,構(gòu)造函數(shù)和析構(gòu)函數(shù)舉例,#include using namespace std; class Point public: Point(int xx,int yy); P

6、oint(); /.其它函數(shù)原形 private: int X,int Y; ; Point:Point(int xx,int yy) X=xx; Y=yy; Point:Point() /.其它函數(shù)的實現(xiàn)略,構(gòu) 造 函 數(shù) 和 析 構(gòu) 函 數(shù),13,構(gòu)造函數(shù)和析構(gòu)函數(shù)調(diào)用舉例,#include using namespace std; class Q int x,y; public: Q(int a=0,int b=0) cout“調(diào)用了構(gòu)造函數(shù)”endl; x=a;y=b; void P(void) coutxtyn; Q() cout“調(diào)用了析構(gòu)函數(shù)!”n;,void main(void

7、) Q q(50,100); q.P(); cout“退出主數(shù)!”n”; ,輸出: 調(diào)用了構(gòu)造函數(shù) 50 100 退出主函數(shù)! 調(diào)用了析構(gòu)函數(shù)!,構(gòu) 造 函 數(shù) 和 析 構(gòu) 函 數(shù),14,類的應(yīng)用舉例,一圓型游泳池如圖所示,現(xiàn)在需在其周圍建一圓型過道,并在其四周圍上柵欄。柵欄價格為35元/米,過道造價為20元/平方米。過道寬度為3米,游泳池半徑由鍵盤輸入。要求編程計算并輸出過道和柵欄的造價。,構(gòu) 造 函 數(shù) 和 析 構(gòu) 函 數(shù),15,#include using namespace std; const float PI = 3.14159; const float FencePrice =

8、35; const float ConcretePrice = 20; class Circle /聲明類Circle 及其數(shù)據(jù)和方法 private: float radius; public: Circle(float r); /構(gòu)造函數(shù) float Circumference(); /圓周長 float Area() ; /圓面積 ; Circle:Circle(float r) /構(gòu)造函數(shù)初始化數(shù)據(jù)成員radius radius=r; float Circle:Circumference() / 計算圓的周長 return 2 * PI * radius; float Circle:A

9、rea() /計算圓的面積 return PI * radius * radius; ,構(gòu) 造 函 數(shù) 和 析 構(gòu) 函 數(shù),16,void main () float radius; float FenceCost, ConcreteCost; coutradius; Circle Pool(radius); /聲明 Circle 對象 Circle PoolRim(radius + 3); FenceCost = PoolRim.Circumference() * FencePrice; /計算柵欄造價并輸出 cout Fencing Cost is ¥ FenceCost endl; Co

10、ncreteCost = (PoolRim.Area() -Pool.Area()*ConcretePrice; /計算過道造價并輸出 cout Concrete Cost is ¥ ConcreteCost endl; ,運行結(jié)果 Enter the radius of the pool: 10 Fencing Cost is ¥2858.85 Concrete Cost is ¥4335.3,構(gòu) 造 函 數(shù) 和 析 構(gòu) 函 數(shù),17,#include “tpoint.h” Tpoint fun(Tpoint q); void main() Tpoint M(12,20),P(0,0),S(

11、0,0); Tpoint N(M); P=fun(N); S=M; cout“P=”P.xcoord()“,”P.ycoord() endl; cout“S=”S.xcoord()“,”S.ycoord() endl; Tpoint fun(Tpoint q) cout“OKn”; int x=q.xcoord()+10; int y=q.ycoord()+15;,18,Tpoint R(x,y); return R; /tpoint.h #include class Tpoint public: Tpoint(int xp,int yp) x=xp; y=yp; Tpoint(Tpoint,

12、19,Tpoint:Tpoint(Tpoint 輸出結(jié)果如下: Copy_initialization constructor called. Copy_initialization constructor called. OK Copy_initialization constructor called. Destructor called. Destructor called. Destructor called.,20,P=22,35 S=12,20 Destructor called. Destructor called. Destructor called. Destructor c

13、alled. 程序輸出結(jié)果說明程序中出現(xiàn)了三次調(diào)用構(gòu)造函數(shù): Tpoint N(M); /M 對象創(chuàng)建N 對象 P=fun(N); /實參N 對象被拷貝到形參q對象 return R; /函數(shù)返回時,調(diào)用拷貝構(gòu)造函數(shù),用對象R創(chuàng)建 / 一個臨時對象,保存R 的數(shù)據(jù),在主函數(shù)中臨 / 時對象被釋放前,將它的內(nèi)容賦值到對象P 中。,臨時對象起暫存作用的情況如下圖所示:,21,組合的概念,類中的成員數(shù)據(jù)是另一個類的對象。 可以在已有的抽象的基礎(chǔ)上實現(xiàn)更復雜的抽象。 通過對復雜對象進行分解、抽象,使我們能夠?qū)⒁粋€復雜對象理解為簡單對象的組合;分解得到復雜對象的部件對象,這些部件對象比它高層的復雜對象更

14、容易理解和實現(xiàn),然后由這些部件對象了“裝配”復雜對象,類 的 組 合,22,例如: #include class Student public: Student() cout“Constructing student.n”; semeshours=100; gpa=3.5; ,23,Student() cout “Destructing student.n”; protected: int semeshours; float gpa; ; class Teacher public: Teacher() cout“Constructing teacher.n”; ,24,Teacher() co

15、ut “Destructing teacher.n”; ; class Tutorpair public: Tutorpair() cout“Constructing tutorpair.n”; nomeetings=0; Tutorpair() cout“Destructing tutorpair.n”; ,25,protected: Student student; Teacher teacher; int nomeetings; ; void main() Tutorpair tp; cout“Back in main.n”; ,Constructing student.,Constru

16、cting teacher.,Constructing tutorpair.,Back in main.,Destructing tutorpair.,Destructing teacher.,Destructing student.,運行結(jié)果:,26,舉例,class Point private: float x,y; /點的坐標 public: Point(float h,float v); /構(gòu)造函數(shù) float GetX(void); /取X坐標 float GetY(void); /取Y坐標 void Draw(void); /在(x,y)處畫點 ; /.函數(shù)的實現(xiàn)略,class L

17、ine private: /線段的兩個端點 Point p1,p2; public: /構(gòu)造函數(shù) Line(Point a,Point b); /畫出線段 Void Draw(void); /.函數(shù)的實現(xiàn)略,類 的 組 合,27,類組合的構(gòu)造函數(shù)設(shè)計,原則:不僅要負責對本類中的基本類型成員數(shù)據(jù)賦初值,也要對對象成員初始化。 聲明形式: 類名:類名(對象成員所需的形參,本類成員形參) :對象1(參數(shù)),對象2(參數(shù)),. 本類初始化 ,類 的 組 合,28,類組合構(gòu)造函數(shù)實例,Point(int xx=0,int yy=0) x=xx;y=yy; Line:Line(Point a,Point

18、b,double p):p1(a),p2(b) double x=double(p1.GetX()-p2.GetX(); double y=double(p1.GetY()-p2.GetY(); dist=sqrt(x*x+y*y); price=p; ,類 的 組 合,29,類組合的構(gòu)造函數(shù)調(diào)用,構(gòu)造函數(shù)調(diào)用順序:先調(diào)用內(nèi)嵌對象的構(gòu)造函數(shù)(按內(nèi)嵌時的聲明順序,先聲明者先構(gòu)造)。然后調(diào)用本類的構(gòu)造函數(shù)。(析構(gòu)函數(shù)的調(diào)用順序相反) 若調(diào)用默認構(gòu)造函數(shù)(即無形參的),則內(nèi)嵌對象的初始化也將調(diào)用相應(yīng)的默認構(gòu)造函數(shù)。,類 的 組 合,30,類的組合舉例1,#include #include using namespace std; class Point private: float x,y; /點的坐標 public: Point(float xx,float yy) cout“Point構(gòu)造函數(shù)”endl; x=xx;y=yy; Point(Point ,類 的 組 合,31,class Distance private: Point p1,p2; /線段的兩個端點 double dist; public: Distance(Point a,Point b); /構(gòu)造函數(shù) double GetDis(void)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 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

提交評論