版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
ObjectivesTounderstandinheritancefurtherTobeabletodefinederivedclassesindifferentinheritancemodes01MemberFunctionsofDerivedClasses03CaseStudy02InheritanceAccessControl01MemberFunctionsofDerivedClassesMemberfunctionsof
DerivedClassesclass
Employee{public:Employee(string
n,int
d){name=n;department=d;}voidprint()const{cout<<name<<department<<endl;}private:stringname;intdepartment;};class
Manager:public
Employee{public:Manager(string
n,int
d,string
rs):Employee(n,d),responsibility(rs){}private:stringresponsibility;};voidprint()const;Manager-responsibility:string+Manager(string,int,string)+management():voidEmployee-name:stringdepartment:string+Employee(string,int)+print():voidconstMemberFunctionsofDerivedClassesThememberfunctionsofthederivedclasscanalsobere-definedwhentheirnamesarethesameasonesofitsbaseclass.class
Manager:public
Employee{public:Manager(string&n,int
d,int
lvl):Employee(n,d),level(lvl){}voidprint()const;private:stringresponsibility;};void
Manager::print(){print();cout<<responsibility;}void
Manager::print(){Employee::print();cout<<responsibility;}void
Manager::print()const{cout<<name<<department;cout<<responsibility;}CannotaccesstheprivatemembersintheotherclassHowtodefinetheprintfunctioninclassManager?√MemberfunctionsofDerivedClassesvoid
Manager::print(){Employee::print();cout<<responsibility;}Functionoverridingisafeaturethat
allowsustouseafunctioninthederivedclassthatisalreadypresentinitsbaseclass.Whenyouwanttooverrideafunctionalityinthebaseclass,youusefunctionoverridingin
thederivedclass.Thismeansthattheimplementationofthebaseclassfunctionsismodified.FunctionOverridingFunctionOverridingvsOverloadingFunctionoverloadingprovidesmultipledefinitionsofthefunctionbychangingsignature,i.e.changenumbersofparametersanddatatypesofparameters,butthereturntypedoesn’tplayanyrole.Functionoverridingisredefinitionofthebaseclassfunctionwiththesamereturntypeandparameterlistinthederivedclass.voidprint();//okvoidprint(int);//okintprint(int);//errorstringprint(string);//okclass
base{public:voidfun(){cout<<
"base::fun()\n";}voidfun(int
a){cout<<
"base::fun(int)"
<<
a
<<endl;}};class
derived:public
base{public:voidfun(){cout<<
“derived::fun()\n";}voidfun(string
s){cout<<
"derived::fun(string)"
<<
s
<<endl;}};intmain(){baseb;b.fun();b.fun(4);derivedd;d.fun();d.fun("4");//d.fun(4);compileerrorreturn0;}OverridingmemberfunctionsOverloadingmemberfunctionsFunctionOverridingvsOverloading02InheritanceAccessControlAccessControlIfitisprivate,itsnamecanbeusedonlybymemberfunctionsandfriendsoftheclassinwhichitisdeclared.Ifitispublic,itsnamecanbeusedbyanyfunction.Ifitisprotected,itsnamecanbeusedonlybymemberfunctionsandfriendsoftheclassinwhichitisdeclaredandbymemberfunctionsandfriendsofclassesderivedfromthisclass.#Test-
a:int+
b:int#
c:intAccesscontrolinaclassareclassifiedintothreecategories:private,protectedorpublic.AccessControl–ProtectedMembersintmain(){Testta;ta.a=99;ta.b=100;ta.c=50;cout<<ta.geta()<<endl;cout<<ta.getb()<<endl;cout<<ta.c<<endl;return0;}//error-
private//error-protected//ok-publicTest-
a:int+
b:int#
c:intclass
Test{inta;protected:intb;public:intc;Test(){a=b=c=10;}intgeta(){returna;}intgetb(){returnb;}};class
dTest:public
Test{public:dTest():Test(){dt=20;}voidreset(){a=20;//errorb=20;}protected:intdt;};intmain(){dTestdtObj;cout<<dtObj.geta();cout<<dtObj.getb();cout<<dtObj.c;dtObj.reset();cout<<dtObj.getb();return0;}InheritanceAccessControlWhencreatingaderivedclassfromabaseclass,youcanusedifferentaccessspecifierstoinheritthedatamembersofthebaseclass.Thederivedclasscanaccessallthenon-privatemembersofitsbaseclass.Andthebaseclassmembersthatarenotaccessibletothememberfunctionsofderivedclassesshouldbedeclaredprivateinthebaseclass.Theaccessspecifiersthatareusedarepublic,privateandprotected.Whenderivingaclassfromabaseclass,thebaseclassmaybeinheritedthroughpublic,privateandprotectedinheritance.PublicInheritancesWheninheritingaclassfromapublicbaseclass,i.e.publicderivation,publicmembers
ofthebaseclassbecomepublicmembers
ofthederivedclass,protectedmembersofthebaseclassbecomeprotectedmembers
ofthederivedclass,andprivatemembersofthebaseclasskeepprivatemembersofthederivedclass.Publicderivationmakesthederivedclassasubtypeofitsbaseclass,itisthemostcommonformofderivation.PublicInheritanceclass
RichMan{public:RichMan();~RichMan();intcompany;//公司能被自己(基類),創(chuàng)業(yè)伙伴(友元),兒子(子類),其他人(外部)都可以訪問private://錢和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問.兒子和外人都不給開int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問,兒子(子類)也可以訪問,外人是不可以訪問};RichMancar:intmoney:int#house:int+company:int+RichMan()+~RichMan()class
LittleRichMan:public
RichMan{public:LittleRichMan();~LittleRichMan();};PublicInheritanceclass
LittleRichMan:public
RichMan{public:LittleRichMan();~LittleRichMan();};publicclass
LittleRichMan:public
RichMan{public:LittleRichMan();~LittleRichMan();intm_company;//baseclassprotected:
intm_house;//baseclassprivate:
intcar;//baseclass
intmoney;//baseclass};LittleRichMan+LittleRichMan()+~LittleRichMan()publicRichMancar:intmoney:int#house:int+company:int+RichMan()+~RichMan()ProtectedInheritancesWhenderivingfromaprotectedbaseclass,i.e.protectedderivation,publicandprotectedmembersofthebaseclassbecomeprotectedmembersofthederivedclass,andprivatemembersofthebaseclasskeepprivatemembersofthederivedclass.Protectedderivationisusefulinclasshierarchiesinwhichfurtherderivationisthenorm.ProtectedInheritancesclass
RichMan{public:RichMan();~RichMan();intcompany;//公司能被自己(基類),創(chuàng)業(yè)伙伴(友元),兒子(子類),其他人(外部)都可以訪問private://錢和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問.兒子和外人都不給開int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問,兒子(子類)也可以訪問,外人是不可以訪問};class
LittleRichMan:protected
RichMan{public:LittleRichMan();~LittleRichMan();};ProtectedInheritanceclass
LittleRichMan:protected
RichMan{public:LittleRichMan();~LittleRichMan();};protectedclass
LittleRichMan:protected
RichMan{public:LittleRichMan();~LittleRichMan();protected:int
m_company;//baseclassint
m_house;//baseclassprivate:int
car;//baseclassint
money;//baseclass};LittleRichMan+LittleRichMan()+~LittleRichMan()protectedRichMancar:intmoney:int#house:int+company:int+RichMan()+~RichMan()PrivateInheritancesWhenwederivefromaprivatebaseclass,i.e.privatederivation,publicandprotectedmembersofthebaseclassbecomeprivatemembersofthederivedclass.Privatemembersofthebaseclasskeepprivatemembersofthederivedclass.Privatederivationismostusefulwhendefiningaclassbyrestrictingtheinterfacetobasesothat
strongerguaranteescanbeprovided.PrivateInheritancesclass
RichMan{public:RichMan();~RichMan();intcompany;//公司能被自己(基類),創(chuàng)業(yè)伙伴(友元),兒子(子類),其他人(外部)都可以訪問private://錢和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問.兒子和外人都不給開int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問,兒子(子類)也可以訪問,外人是不可以訪問};class
LittleRichMan:private
RichMan{public:LittleRichMan();~LittleRichMan();};PrivateInheritanceclass
LittleRichMan:private
RichMan{public:LittleRichMan();~LittleRichMan();};privateclass
LittleRichMan:private
RichMan{public:LittleRichMan();~LittleRichMan();private:intm_company;//baseclassintm_house;//baseclassprivate:intcar;//baseclassintmoney;//baseclass};LittleRichMan+LittleRichMan()+~LittleRichMan()privateRichMancar:intmoney:int#house:int+company:int+RichMan()+~RichMan()DifferentInheritancesBaseclassAccesscontrolDerivedclassAccesscontrolGeneralusersprivateinheritanceprivateprivateNANAprotectedprivateANApublicprivateANAprotectedinheritanceprivateprivateNANAprotectedprotectedANApublicprotectedANApublicinheritanceprivateprivateNANAprotectedprotectedANApublicpublicAA03CaseStudyCaseStudyDesignasystemthatopensdifferentpostfixedfiles,e.g..txt,.pngandetc.textFile+openFile():voidFile#filename:string+openFile():voidbmpFile+openFile():voidDataabstractionForatextfilewith.txt,data:filenamefunction:openfileForanimagefilewith.png,data:filenamefunction:openfileAbstractcommonfeaturesCaseStudyclass
File{public:File(string&filename){fileName=filename;}voidopenFile(){cout<<"Openfile!\n";}protected:stringfileName;};class
textFile
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度工業(yè)廠房交易全程服務(wù)合同4篇
- 2024音樂制作方與影視制作公司版權(quán)許可合同
- 二零二五年度交通樞紐害蟲防治與消毒作業(yè)合同3篇
- 專業(yè)水電安裝及消防系統(tǒng)承包合同2024年版版B版
- 2025年度12年首次智慧旅游項目合作協(xié)議3篇
- 2025年度叉車租賃合同范本(叉車租賃與維護(hù))4篇
- 2025年度智慧城市基礎(chǔ)設(shè)施場地平整與物聯(lián)網(wǎng)協(xié)議4篇
- 2025年度奶牛養(yǎng)殖牛場租賃合同范本3篇
- 2025年廠房租賃合同風(fēng)險評估與管理規(guī)范4篇
- 2024年04月廣西桂林銀行南寧分行社會招考筆試歷年參考題庫附帶答案詳解
- DB32T-經(jīng)成人中心靜脈通路裝置采血技術(shù)規(guī)范
- 【高空拋物侵權(quán)責(zé)任規(guī)定存在的問題及優(yōu)化建議7100字(論文)】
- TDALN 033-2024 學(xué)生飲用奶安全規(guī)范入校管理標(biāo)準(zhǔn)
- 物流無人機(jī)垂直起降場選址與建設(shè)規(guī)范
- 冷庫存儲合同協(xié)議書范本
- AQ/T 4131-2023 煙花爆竹重大危險源辨識(正式版)
- 武術(shù)體育運(yùn)動文案范文
- 設(shè)計服務(wù)合同范本百度網(wǎng)盤
- 2024年市級??谱o(hù)士理論考核試題及答案
- 肺炎臨床路徑
- 供應(yīng)商供貨服務(wù)方案(2篇)
評論
0/150
提交評論