




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 南京農(nóng)業(yè)大學《商務(wù)應(yīng)用文寫作》2023-2024學年第二學期期末試卷
- 吉利學院《電波傳播概論雙語》2023-2024學年第二學期期末試卷
- 河北東方學院《數(shù)字信號處理課程設(shè)計實訓(xùn)》2023-2024學年第二學期期末試卷
- 香港科技大學(廣州)《新藥研發(fā)的關(guān)鍵技術(shù)》2023-2024學年第二學期期末試卷
- 武漢電力職業(yè)技術(shù)學院《創(chuàng)新思維與教育創(chuàng)新創(chuàng)業(yè)》2023-2024學年第二學期期末試卷
- 昆玉職業(yè)技術(shù)學院《混凝土結(jié)構(gòu)與性能A》2023-2024學年第二學期期末試卷
- 醫(yī)用紅外熱像儀項目效益評估報告
- Unit 5 The Monarch's Journey Understanding ideas 教學設(shè)計-2024-2025學年高中英語外研版(2019)必修第一冊
- 漳州城市職業(yè)學院《模式識別技術(shù)應(yīng)用》2023-2024學年第二學期期末試卷
- 湖南民族職業(yè)學院《化工軟件概論》2023-2024學年第二學期期末試卷
- 烹飪美學PPT完整全套教學課件
- DSH中的常見語法考點
- 白酒廠安全檢查表
- 2023年民辦中學三重一大事項決策規(guī)定
- 公路工程崗位安全操作規(guī)程
- 起重裝卸機械操作工國家職業(yè)技能標準(2023年版)
- 學校開學安全檢查記錄表
- 中醫(yī)學理論體系的基本特點(中醫(yī)基礎(chǔ)理論課件)
- 大象版科學六年級下冊全冊教材整體分析(課件)
- 2022年08月銀清科技有限公司招聘24筆試參考題庫答案詳解版
- 《第十一課-絮絮叨叨滿是情課件》小學心理健康教育
評論
0/150
提交評論