Inheritance
Inheritance
• Inheritanceallowsyoutotakeanexistingclassandaddontowhatis
doneinit
• Let'sstartwithanexample,andthentalkaboutwhat'sgoingon
Vocab
• Subclass
• Childclass
• Superclass
• Parentclass
• Inheritancehierarchy
• Baseclass
Initializerlist
Accessmodifiers
• public:
• Everyonecanreadandwritethesevariables,andcallthesefunctions
• protected:
• Thisclassandallsubclassescanreadandwritevariables,andcallfunctions
• protected:
• Only thisclass,notsubclasses,canread/writevariables,callfunctions
StaticDispatch
• Whenafunctioniscalled,thespecificimplementationtobeusedis
determinedatcompiletime
orvirtualvoidreact()override{
Dynamicdispatch
• Thespecificfunctionimplementationtouseforafunctioncallis
resolvedatruntime
• Polymorphism:
• "Inprogramminglanguagesandtypetheory,polymorphism(fromGreek
πολύς,polys,"many,much"andμορφή,morphē,"form,shape")isthe
provisionofasingleinterfacetoentitiesofdifferenttypes.[1]Apolymorphic
typeisonewhoseoperationscanalsobeappliedtovaluesofsomeother
type,ortypes.[2]"(Thankyou,Wikipedia)
Dynamicdispatch
• Thespecificfunctionimplementationtouseforafunctioncallis
resolvedatruntime
• Polymorphism:
• "Inprogramminglanguagesandtypetheory,polymorphism(fromGreek
πολύς,polys,"many,much"andμορφή,morphē,"form,shape")isthe
provisionofasingleinterfacetoentitiesofdifferenttypes.[1]Apolymorphic
typeisonewhoseoperationscanalsobeappliedtovaluesofsomeother
type,ortypes.[2]"(Thankyou,Wikipedia)
• Theactualfunctionexecutedfromafunctioncalldependsonthe
types involvedinthatfunctioncall
Dynamicdispatch:how??
• Ifaclasshasatleastonevirtualfunction,itwillhaveavtable
• Eachinstanceofaclasswithavirtualmethodwillhaveapointerto
thevtable storedinsideofit
• Whenavirtualfunctioniscalled,thevtable pointerischecked
• Thevtable associatesaspecificfunctionimplementationwitheach
virtualfunctionthattheobjecthas
• Basedonthevalueinthevtable,theappropriatefunctioniscalled
Purevirtualfunction:Avirtualfunctionwithexplicitlynoimplementation
Abstractclass:Aclasswithatleastonepurevirtualmemberfunction
Abstractclass:Aclasswithatleastonepurevirtualmemberfunction
Pureabstractclass:Aclasswithonly purevirtualmemberfunctions
ConstructorsandDestructors
• Constructorsarerunfromthetopdownintheinheritanceheirarchy
• Destructorsarerunfromthebottomup
Pet
Dog
Terrier
Cat
© Copyright 2026 Paperzz