C++面向?qū)ο蟪绦蛟O(shè)計雙語教程(第3版)課件 class 11-Inheritance-2_第1頁
C++面向?qū)ο蟪绦蛟O(shè)計雙語教程(第3版)課件 class 11-Inheritance-2_第2頁
C++面向?qū)ο蟪绦蛟O(shè)計雙語教程(第3版)課件 class 11-Inheritance-2_第3頁
C++面向?qū)ο蟪绦蛟O(shè)計雙語教程(第3版)課件 class 11-Inheritance-2_第4頁
C++面向?qū)ο蟪绦蛟O(shè)計雙語教程(第3版)課件 class 11-Inheritance-2_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論