




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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è)伙伴(友元),兒子(子類),其他人(外部)都可以訪問(wèn)private://錢(qián)和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn).兒子和外人都不給開(kāi)int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn),兒子(子類)也可以訪問(wèn),外人是不可以訪問(wèn)};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è)伙伴(友元),兒子(子類),其他人(外部)都可以訪問(wèn)private://錢(qián)和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn).兒子和外人都不給開(kāi)int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn),兒子(子類)也可以訪問(wèn),外人是不可以訪問(wèn)};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è)伙伴(友元),兒子(子類),其他人(外部)都可以訪問(wèn)private://錢(qián)和車子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn).兒子和外人都不給開(kāi)int
money;intcar;protected:int
house;//房子能被自己(基類),創(chuàng)業(yè)伙伴(友元)可以訪問(wèn),兒子(子類)也可以訪問(wèn),外人是不可以訪問(wèn)};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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 餐飲企業(yè)品牌戰(zhàn)略保密與實(shí)施指導(dǎo)合同范本
- 長(zhǎng)租公寓租賃合同模板品質(zhì)生活新選擇
- 村組織員考試題庫(kù)及答案
- 美術(shù)消防員課件下載
- 美術(shù)創(chuàng)意兒童課件全套
- 3月份安全事故案例
- 安全事故應(yīng)急預(yù)案培訓(xùn)心得
- 林業(yè)安全生產(chǎn)工作制度
- 橋梁安全保護(hù)區(qū)管理制度
- 月安全生產(chǎn)檢查方案
- 攝影入門(mén)基礎(chǔ)知識(shí) 課件
- 工程設(shè)計(jì)費(fèi)收費(fèi)標(biāo)準(zhǔn)
- 《正弦函數(shù)的圖像與性質(zhì)》課件
- 人教A版高中數(shù)學(xué)《數(shù)列的概念》優(yōu)秀1課件
- 海姆立克急救(生命的擁抱)課件
- PE管道安裝單元工程質(zhì)量評(píng)定表
- 部編版小學(xué)語(yǔ)文二升三暑假銜接專項(xiàng)訓(xùn)練—看圖寫(xiě)話含例文
- 河道生態(tài)護(hù)岸設(shè)計(jì)概況
- 光伏組件開(kāi)路電壓測(cè)試記錄
- 應(yīng)急預(yù)案演練記錄表范例
- 鐵程檢用表(共47頁(yè))
評(píng)論
0/150
提交評(píng)論