版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
5.2派生類的構(gòu)造函數(shù)與析構(gòu)函數(shù)引例:派生類的定義#include<iostream.h>classbase{ intx;protected: inty;public: voidsetXY(intm,intn){x=m;y=n;} voidshowXY(){cout<<"x="<<x<<endl; cout<<"y="<<y<<endl;}};classderived:publicbase{ intz;public:voidsetXYZ(intm,intn,intk){setXY(m,n);z=k;}voidshowXYZ(){showXY();cout<<"z="<<z<<endl;}};basederivedbase中的setXY改用構(gòu)造函數(shù)后,derived中該如何初始化基類成員?5.2.1定義格式1.派生類構(gòu)造函數(shù)的定義格式:設(shè)類Y由類X派生而來,則類Y的構(gòu)造函數(shù)一般形式為:Y::Y(參數(shù)表0):X(參數(shù)表1){//派生類數(shù)據(jù)成員初始化}2.派生類的析構(gòu)函數(shù)由于析構(gòu)函數(shù)不帶參數(shù),在派生類中是否要定義析構(gòu)函數(shù)與基類無關(guān)。基類的析構(gòu)函數(shù)不會因為派生類沒有析構(gòu)函數(shù)而得不到執(zhí)行,它們各自是獨立的。5.2.1定義格式改寫引例#include<iostream.h>classbase{ intx;protected: inty;public:
base(intm,intn){x=m;y=n;} voidshowXY(){cout<<"x="<<x<<endl; cout<<"y="<<y<<endl;}};classderived:publicbase{ intz;public:derived(intm,intn,intk){setXY(m,n);z=k;}voidshowXYZ(){showXY();cout<<"z="<<z<<endl;}};:base(m,n){z=k;}classA{inta;public:A(intx){a=x;}};classB:publicA{intb;public:B(intx,inty):A(x),b(y){}};A(x){b=y;}例5-2-1:構(gòu)造函數(shù)定義示例當(dāng)基類構(gòu)造函數(shù)不帶參數(shù)時,派生類可以不定義構(gòu)造函數(shù)。classA{inta;public:
A(){a=10;}};classB:publicA{public:intb;……};5.2.1定義格式說明而當(dāng)基類構(gòu)造函數(shù)帶參數(shù)時,它所有的派生類都必須定義構(gòu)造函數(shù),且參數(shù)不能為空。classA{inta;public:A(intx){a=x;}};classB:publicA{intb;public:B(intx,inty):A(x),b(y){}};classC:publicA{public:C(intx):A(x){}};5.2.1定義格式說明續(xù)前頁5.2.1定義格式說明續(xù)前頁若基類構(gòu)造函數(shù)沒有參數(shù),則派生類的構(gòu)造函數(shù)中可以略去“:基類名(參數(shù)表)”。classA{inta;public:
A(){a=10;}};classB:publicA{intb;public:B(intx){b=x;}};如果派生類的基類也是一個派生類,每個派生類只需負(fù)責(zé)其直接基類的構(gòu)造。classA{inta;public:A(intx){a=x;}};classB:publicA{intb;public:B(intx,inty):A(x),b(y){}};classC:publicB{intc;public:C(intx,inty,intz):B(x,y),c(z){}};5.2.1定義格式
說明續(xù)前頁5.2.2調(diào)用順序當(dāng)說明一個派生類對象時,先調(diào)用基類的構(gòu)造函數(shù),然后調(diào)用派生類的構(gòu)造函數(shù),如果基類仍然時一個派生類,則這個過程遞歸進行。析構(gòu)函數(shù)調(diào)用順序與構(gòu)造函數(shù)的相反。派生類還包含對象成員時,先調(diào)用基類構(gòu)造函數(shù),再調(diào)用對象成員的構(gòu)造函數(shù)。例5-2-2:調(diào)用順序示例#include<iostream.h>classbase{inta;public:base(intx){a=x;cout<<"constructingbase”<<endl;}~base(){cout<<"destroyingbase“<<endl;}};classderived:publicbase{intd;public:derived(intx,inty):base(x){d=y;cout<<"constructingderived“<<endl;}~derived(){cout<<"destroyingderived"<<endl;}};main(){derivedd(5,8);}例5-2-2:調(diào)用順序示例5.2.2程序運行結(jié)果constructingbaseconstructingderiveddestroyingderiveddestroyingbase改寫derived5.2.2classderived:publicbase{intd;basemember;public:derived(intx,inty,intz):base(x),member(y){d=z;cout<<”constructingderived”<<endl;}~derived(){cout<<”destroyingderived”<<endl;}main(){derivedd(5,8,10);}程序運行結(jié)果:5.2.2constructingbaseconstructingbaseconstructingderiveddestroyingderiveddestroyingbasedestroyingbase例5-2-3:正多邊形的繼承5.2.2分析:所有正多邊形都有中心點和顏色兩個屬性,有移動、改變顏色、打印和求面積四個行為。shapecirclerectangletriangleshape:center、colormove()changeColor()print()area()圓有半徑,它有自己的求面積和打印方法。矩形有寬和高,它有自己的求面積和打印方法。三角形有底邊(寬)和高,它有自己的求面積和打印方法。classpoint{ intx,y;public: point(inta=0,intb=0){x=a;y=b;}pointgetPoint(){return*this;} voidsetPoint(pointp){x=p.x;y=p.y;} voidshow(){cout<<"("<<x<<","<<y<<")"<<endl;}};classshape{protected:pointcenter;charcolor[10];public:shape(intx,inty,char*c):center(x,y){strcpy(color,c);}voidmove(pointp){center.setPoint(p);}voidprint(){cout<<"Thisisa"<<color<<"shape,centerof";center.show();}voidchangeColor(char*c){strcpy(color,c);}voidarea(){cout<<"Theareaofshapeis0.0“<<endl;}};例5-2-3續(xù)classcircle:publicshape{ intradius;public:circle(intx,inty,char*c,intr):shape(x,y,c){radius=r;}voidprint(){cout<<"Thisisa“<<color<<"circle,centerof"; center.show();}voidarea(){cout<<"Theareaofcircleis“<<3.14*radius*radius<<endl;}};classshape{protected:pointcenter;charcolor[10];public:
shape(intx,inty,char*c):center(x,y){strcpy(color,c);}voidmove(pointp){center.setPoint(p);}voidprint(){cout<<"Thisisa"<<color<<"shape,centerof";center.show();}voidchangeColor(char*c){strcpy(color,c);}voidarea(){cout<<"Theareaofshapeis0.0“<<endl;}};例5-2-3續(xù)classrectangle:publicshape{protected: intwidth,height;public:
rectangle(intx,inty,char*c,intw,inth):shape(x,y,c){width=w;height=h;}voidprint(){cout<<"Thisisa"<<color<<"rectangle,centerof"; center.show();}voidarea(){cout<<"Theareaofrectangleis"<<width*height<<endl;}};classshape{protected:pointcenter;charcolor[10];public:
shape(intx,inty,char*c):center(x,y){strcpy(color,c);}voidmove(pointp){center.setPoint(p);}voidprint(){cout<<"Thisisa"<<color<<"shape,centerof";center.show();}voidchangeColor(char*c){strcpy(color,c);}voidarea(){cout<<"Theareaofshapeis0.0“<<endl;}};例5-2-3續(xù)classtriangle:publicrectangle{public:
triangle(intx,inty,char*c,intw,inth):rectangle(x,y,c,w,h){}voidprint(){cout<<"Thisisa"<<color<<"triangle,centerof"; center.show();}voidarea(){cout<<"Theareaoftriangleis"<<width*height/2<<endl;}};classrectangle:publicshape{protected: intwidth,height;public:
rectangle(intx,inty,char*c,intw,inth):shape(x,y,c){width=w;height=h;}voidprint(){cout<<"Thisisa"<<color<<"rectangle,centerof"; center.show();}voidarea(){cout<<"Theareaofrectangleis"<<width*height<<endl;}};例5-2-3續(xù)main(){ circlec(50,50,"red",50); c.print(); c.area(); c.move(point(100,100)); c.changeColor("blue"); c.print(); rectangler(10,10,"yellow",10,30); r.print(); r.area(); r.move(point(20,20));r.changeColor("green");r.print();trianglet(20,20,"white",40,50);t.print();t.area();t.move(point(30,30));t.changeColor("black");t.print(); }例5-2-3續(xù)繼承是重用的基礎(chǔ)面向?qū)ο蟮某绦蛟O(shè)計中通過繼承實現(xiàn)重用reuse——便于重用,擴充??梢栽谠械幕A(chǔ)上擴充,不修改原有代碼。即使原來的代碼完全不符合當(dāng)前的要求,也不必修改原有的代碼??梢灾辉黾有碌拇a來實現(xiàn)新的功能。覆蓋與支配規(guī)則函數(shù)覆蓋:在派生類中可以重新定義基類中已定義的同名函數(shù),我們稱之為函數(shù)覆蓋。支配規(guī)則:派生類的對象引用某函數(shù)時,編譯器先在派生類中找與該函數(shù)完全匹配的函數(shù),有則調(diào)用之;否則到其基類中找,依次再到基類的基類中找,……等等,若都沒有找到則出錯。
溫馨提示
- 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)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 多式聯(lián)運物流行業(yè)三年發(fā)展洞察報告
- 電視節(jié)目廣告植入行業(yè)影響因素分析
- 在線繪畫教育行業(yè)營銷策略方案
- 工業(yè)廢棄物處理行業(yè)市場突圍建議書
- 工業(yè)互聯(lián)網(wǎng)行業(yè)市場機遇分析
- 油氣管道施工與安裝行業(yè)研究報告
- 多元文化美食行業(yè)市場需求分析及未來三年行業(yè)預(yù)測報告
- 跨境同城物流行業(yè)市場機遇分析
- 可降解材料行業(yè)投資機會與風(fēng)險識別及應(yīng)對策略報告
- 《 華勘公司員工績效考核體系研究》范文
- 電商平臺服務(wù)協(xié)議、交易規(guī)則【微信小程序申請模板】
- 產(chǎn)品性能分析對比報告
- 國際客票銷售業(yè)務(wù)培訓(xùn)
- 社區(qū)衛(wèi)生服務(wù)管理的基本理論和方法
- 典范英語第五冊
- 常用地質(zhì)圖例及符號
- 消防施工進度計劃橫道圖
- 材料價格確認(rèn)單.doc
- 導(dǎo)尿知情同意書.doc
- 車架系統(tǒng)設(shè)計規(guī)范標(biāo)準(zhǔn)版
- 第8章品牌管理[章節(jié)講課]
評論
0/150
提交評論