2006PearsonEducation,Inc.Allrightsreserved.112Object-OrientedProgramming:Inheritance2006PearsonEducation,Inc.Allrightsreserved.2OBJECTIVESInthischapteryouwilllearn:Tocreateclassesbyinheritingfromexistingclasses.Howinheritancepromotessoftwarereuse.Thenotionsofbaseclassesandderivedclassesandtherelationshipsbetweenthem.Theprotectedmemberaccessspecifier.Theuseofconstructorsanddestructorsininheritancehierarchies.Thedifferencesbetweenpublic,protectedandprivateinheritance.Theuseofinheritancetocustomizeexistingsoftware.2006PearsonEducation,Inc.Allrightsreserved.312.1Introduction12.2BaseClassesandDerivedClasses12.3protectedMembers12.4RelationshipbetweenBaseClassesandDerivedClasses12.4.1CreatingandUsingaCommissionEmployeeClass12.4.2CreatingaBasePlusCommissionEmployeeClassWithoutUsingInheritance12.4.3CreatingaCommissionEmployee-BasePlusCommissionEmployeeInheritanceHierarchy12.4.4CommissionEmployee-BasePlusCommissionEmployeeInheritanceHierarchyUsingprotectedData12.4.5CommissionEmployee-BasePlusCommissionEmployeeInheritanceHierarchyUsingprivateData12.5ConstructorsandDestructorsinDerivedClasses12.6public,protectedandprivateInheritance12.7SoftwareEngineeringwithInheritance12.8Wrap-UpHenanUniversityofTechnology2020/1/25412.1IntroductionInheritanceisaformofsoftwarereuseinwhichtheprogrammercreatesaclassthatabsorbsanexistingclass’sdataandbehaviorsandenhancesthemwithnewcapabilities.Theexistingclassiscalledthebaseclass(superclass),thenewclassiscalledderivedclass(subclass).HenanUniversityofTechnology2020/1/2551.SeveralConceptsDirectbaseclassthebaseclassfromwhichaderivedclassexplicitlyinherits.Indirectbaseclassthebaseclasswhichisinheritedformtwoormorelevelsintheclasshierarchy.SingleinheritanceAclassisderivedfromonebaseclassMultipleinheritanceAclassisderivedfrommultiplebaseclassHenanUniversityofTechnology2020/1/256PublicinheritanceEveryobjectofaderivedclassisalsoanobjectofthecorrespondingbaseclass.however,baseclassarenotobjectsoftheirderivedclass.PrivateinheritanceInsomecases,thisformcanbeanalternativetocomposition.ProtectedinheritanceThisformisrarelyusedMemberfunctionsofaderivedclasscannotdirectlyaccessprivatemembersofthebaseclass.2.ThreeKindsOfInheritanceHenanUniversityofTechnology2020/1/2573.is-arelationshipvs.has-arelationshipTheis-arelationshiprepresentsinheritance.Anobjectofaderivedclassalsocanbetreatedasanobjectofitbaseclass.Thehas-arelationshiprepresentscomposition.AnobjectcontainsoneormoreobjectsofotherclassesasmembersHenanUniversityofTechnology2020/1/25812.2BaseClassesandDerivedClassesOnebaseclasscanhavemanyderivedclass.BaseclassDerivedclassesStudentGraduateStudent,UndergraduateStudentShapeCircle,Triangle,Rectangle,Sphere,CubeLoanCarLoan,HomeImprovementLoan,MortgageLoanEmployeeFaculty,StaffAccountCheckingAccount,SavingsAccountHenanUniversityofTechnology2020/1/259Inheritancerelationshipsformtreelikehierarchystructures.Aclassbecomeseitherabaseclass---supplyingmemberstootherclass,aderivedclass----inheritingitsmembersfromotherclasses,orboth.Fig.12.2|InheritancehierarchyforuniversityCommunityMembers.Eacharrowrepresentsthe“is-a”relationshipHenanUniversityofTechnology2020/1/2510Fig.12.3|InheritancehierarchyforShapes.ClassTwoDimensionalShape:publicShapePublicinheritanceHenanUniversityofTechnology2020/1/2511PublicInheritanceWithallformsofinheritance,privatemembersofabaseclassarenotaccessiblefromthatclass’sderivedclass,buttheseprivatemembersarestillinherited.Withpublicinheritance,allotherbase-classmembersretaintheiroriginalmemberaccesswhentheybecomemembersofthederivedclass.Friendfunctionsarenotinherited.HenanUniversityofTechnology2020/1/251212.3ProtectedMembersAbaseclass’sprotectedmemberscanbeaccessedwithinthebodyofthatbaseclass,bymembersandfriendsofthatbaseclass,andbymembersandfriendsofanyclassesderivedformthatbaseclass.HenanUniversityofTechnology2020/1/251312.4RelationshipbetweenBaseClassesandDerivedClassesCommissionemployee&base-salariedcommissionemployeeWedivideourdiscussionofrelationshipbetweenCommissionemployeesandbase-salariedcommissionemployeesof5examples:HenanUniversityofTechnology2020/1/251412.4.1The1thexampleCreatingandUsingaCommissionEmployeeClassP412HenanUniversityofTechnology2020/1/25151//Fig.12.4:CommissionEmployee.h2//CommissionEmployeeclassdefinitionrepresentsacommissionemployee.3#ifndefCOMMISSION_H4#defineCOMMISSION_H56#includestring//C++standardstringclass7usingstd::string;89classCommissionEmployee10{11public:12CommissionEmployee(conststring&,conststring&,conststring&,13double=0.0,double=0.0);1415voidsetFirstName(conststring&);//setfirstname16stringgetFirstName()const;//returnfirstname1718voidsetLastName(conststring&);//setlastname19stringgetLastName()const;//returnlastname2021voidsetSocialSecurityNumber(conststring&);//setSSN22stringgetSocialSecurityNumber()const;//returnSSN2324voidsetGrossSales(double);//setgrosssalesamount25doublegetGrossSales()const;//returngrosssalesamount2627voidsetCommissionRate(double);//setcommissionrate(percentage)28doublegetCommis