Electrical Engineering - Chapter 15: Inheritance, Virtual Functions, and Polymorphism

Name of another way in which classes and objects relate Base class Derived class When object of derived class declared Object of base class is automatically created and contained within derived class object

ppt21 trang | Chia sẻ: thuongdt324 | Lượt xem: 393 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 15: Inheritance, Virtual Functions, and Polymorphism, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 15 – Inheritance, Virtual Functions, and PolymorphismInheritanceName of another way in which classes and objects relateBase classDerived classWhen object of derived class declaredObject of base class is automatically created and contained within derived class objectLesson 15.1Image of base class object creation when derived class object instantiatedLesson 15.1Derived class objectBase class objectDefining a Base ClassLesson 15.1class Base { private: type base_priv_dat; protected: type base_prot_dat; public: type base_function_name ( ); };Defining a Derived ClassLesson 15.1class Derived: public Base { private: int derived_priv_dat; public: void derived_function_name ( ); };Interaction Between Base and Derived Class ObjectsPrivate base class members are private member of base class onlyProtected base class members are protected members of both derived and base classesPublic base class members are public members of derived and base classesDerived class members convey no special access privileges to base class functionsLesson 15.1Relationship Between Base and Derived ObjectsDerived objectBase objectprivate dataprotected datapublic functionsprivate datapublic functionsderived_object.derived_function( )derived_object.base_function( )No Base Class Object NameUse only derived object name to access both derived and base class functionsDerived class takes precedenceover-riding base class functionLesson 15.1Comparison of Types of Class and Object InteractionsFriend classobjectGranting class objectClient classobjectServer classobjectComponentclass objectComposite ClassObjectDerived class object Base class objectLesson 15.1On all examples, upper boxes representprivate data members and lower boxesrepresent public member functions.Private and Protected Inheritanceclass Derived : private Basepublic and protected members of Base become private members of Derivedprivate members of Base remain private to Baseclass Derived : protected Basepublic and protected members of Base become public and protected member of Derivedprivate members of Base remain private to BaseLesson 15.1Order of Constructor and Destructor Function CallsBoth base and derived constructors called when derived class object instantiatedBass class constructor called first, derived constructor secondDerived class destructor called first, base class destructor secondOrder in which objects declared determines order of constructor callsLesson 15.2Other Uses of InheritanceWell suited to extending or customizing existing codeCan make full use of base class features and add specific featuresCan modify base classes without requiring derived classes to be modifiedAllows base classes to remain up to date Lesson 15.1Explicit Call to Base Class ConstructorLesson 15.2 Derived object (value1, value2);Derived :: Derived (type derived_var, type base_var) :derived_member (derived_var), Base (base_var)CallConstructorEffect of Inheritance LevelsObject for lowest level class contains sub-objects of all other classesOrder of calling constructors is from top down in inheritance graphPass data one step or level at a timeFunctions can be overridden in any class down inheritance graphCan assign derived class objects to base class objects, but not reverseLesson 15.3Effect of Inheritance LevelsObject for lowest level class contains sub-objects of all other classesLesson 15.3Earth objectPlanet object(no name)Celestial_bodyobject(no name)Multiple InheritanceClass can inherit from more than one class directlyWhen one class has "is a" relationship with more than one classLesson 15.4Virtual ClassesClass which we are not allowed to create independent objectsUsed as base class for other classes for which we do want independent objectsC++ automatically makes class virtual when member of class is pure virtual functionLesson 15.5virtual type function (type, type) = 0;Pointers and InheritanceCan declare base class type pointer variable and assign address of derived class objectAny address of object down inheritance graphLesson 15.5BindingAssociation of function call with a functionEarly bindingOccurs during compilationAlso called static or compile-time bindingLate bindingOccurs during executionAlso called dynamic or run-time bindingLesson 15.5PolymorphismAchieved by creating and using virtual functionsCode making function call does not explicitly state which function it is callingC++ decides during execution which is correct functionSummaryCreate an inheritance hierarchyUse inheritance as a form of class interactionMake a virtual classUse polymorphism in an engineering programLearned how to:
Tài liệu liên quan