




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
5.2派生類的構造函數(shù)與析構函數(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改用構造函數(shù)后,derived中該如何初始化基類成員?5.2.1定義格式1.派生類構造函數(shù)的定義格式:設類Y由類X派生而來,則類Y的構造函數(shù)一般形式為:Y::Y(參數(shù)表0):X(參數(shù)表1){//派生類數(shù)據(jù)成員初始化}2.派生類的析構函數(shù)由于析構函數(shù)不帶參數(shù),在派生類中是否要定義析構函數(shù)與基類無關?;惖奈鰳嫼瘮?shù)不會因為派生類沒有析構函數(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:構造函數(shù)定義示例當基類構造函數(shù)不帶參數(shù)時,派生類可以不定義構造函數(shù)。classA{inta;public:
A(){a=10;}};classB:publicA{public:intb;……};5.2.1定義格式說明而當基類構造函數(shù)帶參數(shù)時,它所有的派生類都必須定義構造函數(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ù)前頁若基類構造函數(shù)沒有參數(shù),則派生類的構造函數(shù)中可以略去“:基類名(參數(shù)表)”。classA{inta;public:
A(){a=10;}};classB:publicA{intb;public:B(intx){b=x;}};如果派生類的基類也是一個派生類,每個派生類只需負責其直接基類的構造。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調用順序當說明一個派生類對象時,先調用基類的構造函數(shù),然后調用派生類的構造函數(shù),如果基類仍然時一個派生類,則這個過程遞歸進行。析構函數(shù)調用順序與構造函數(shù)的相反。派生類還包含對象成員時,先調用基類構造函數(shù),再調用對象成員的構造函數(shù)。例5-2-2:調用順序示例#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:調用順序示例5.2.2程序運行結果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);}程序運行結果: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ù)繼承是重用的基礎面向對象的程序設計中通過繼承實現(xiàn)重用reuse——便于重用,擴充??梢栽谠械幕A上擴充,不修改原有代碼。即使原來的代碼完全不符合當前的要求,也不必修改原有的代碼。可以只增加新的代碼來實現(xiàn)新的功能。覆蓋與支配規(guī)則函數(shù)覆蓋:在派生類中可以重新定義基類中已定義的同名函數(shù),我們稱之為函數(shù)覆蓋。支配規(guī)則:派生類的對象引用某函數(shù)時,編譯器先在派生類中找與該函數(shù)完全匹配的函數(shù),有則調用之;否則到其基類中找,依次再到基類的基類中找,……等等,若都沒有找到則出錯。
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 足療店成本分析與財務決策支持系統(tǒng)研究
- 酒店旅游團隊用餐的菜單策劃與實踐案例分享
- 跨國企業(yè)知識產權培訓的必要性及方法
- 跨平臺設計軟件的發(fā)展與應用分析
- 北京專版2024年中考生物復習主題訓練四
- 資金管理與企業(yè)價值的創(chuàng)造過程
- 閱讀理解能力與思維能力的提升
- 新課標2024年中考生物復習第四單元生物圈中的人課時訓練14血液血管和血型輸血
- 足球教學理念在青少年培養(yǎng)中的應用
- 浙江國企招聘2024下半年臺州玉環(huán)市國有企業(yè)公開招聘38人筆試參考題庫附帶答案詳解
- DB1331T 102-2025雄安新區(qū)應急物資儲備庫建設規(guī)范
- 北京市豐臺區(qū)2024-2025學年九年級上學期期末道德與法治試題(含答案)
- 醫(yī)院培訓課件:《PET-CT的臨床應用》
- 《從外觀看豬病診治》課件
- 《莫比烏斯環(huán)》課件
- 2025海南省交通投資控股限公司招聘30人高頻重點提升(共500題)附帶答案詳解
- 《工業(yè)機器人現(xiàn)場編程》課件-任務3.涂膠機器人工作站
- 富饒的西沙群島課件
- 中英文對照版 ASTM B594 2019 鋁合金形變產品超聲波檢測標準慣例
- 五年級下冊道德與法治教學計劃
- 2025屆高考數(shù)學專項復習:阿基米德三角形【六大題型】含答案
評論
0/150
提交評論