Lecture Programming in C++ - Chapter 15: Inheritance, virtual functions, and polymorphism. In this chapter, you will learn to: Create an inheritance hierarchy, use inheritance as a form of class interaction, make a virtual class, use polymorphism in an engineering program.
Trang 1Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Trang 2Name 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
Lesson 15.1
Trang 6Derived class members convey no special access privileges to base class functions
Lesson 15.1
Trang 7Base and Derived Objects
Derived object Base object
private data protected data public functions
private data
public functions
derived_object.derived_function( )
derived_object.base_function( )
Trang 8Use only derived object name to access both derived and base class functions
Derived class takes precedence
– overriding base class function
Lesson 15.1
Trang 9Component class object
Composite Class
Object
Derived class object Base class object
Lesson 15.1
On all examples, upper boxes represent private data members and lower boxes represent public member functions.
Trang 10class Derived : private Base
– public and protected members of Base become private members of Derived
– private members of Base remain private to Base
class Derived : protected Base
– public and protected members of Base become public and protected member of Derived
– private members of Base remain private to Base
Lesson 15.1
Trang 11Order of Constructor and Destructor Function Calls
Both base and derived constructors called when derived class object instantiated
Bass class constructor called first, derived constructor second
Derived class destructor called first, base class destructor second
Order in which objects declared determines order of constructor calls
Lesson 15.2
Trang 12Well suited to extending or customizing
existing code
Can make full use of base class features and add specific features
Can modify base classes without requiring derived classes to be modified
– Allows base classes to remain up to date
Lesson 15.1
Trang 13Constructor
Trang 14Object for lowest level class contains subobjects of all other classes
Order of calling constructors is from top down in inheritance graph
Pass data one step or level at a time
Functions can be overridden in any class down inheritance graph
Can assign derived class objects to base class objects, but not reverse
Lesson 15.3
Trang 15Object for lowest level class contains subobjects of all other classes
Lesson 15.3 Earth object
Planet object (no name)
Celestial_body
object (no name)
Trang 16Class can inherit from more than one class directly
– When one class has "is a" relationship with more than one class
Lesson 15.4
Trang 17virtual type function (type, type) = 0;
Trang 18Can declare base class type pointer variable and assign address of derived class object
Any address of object down inheritance
graph
Lesson 15.5
Trang 19Association of function call with a functionEarly binding
Trang 21Learned how to: