13 • Object-OrientedProgramming:Polymorphism
One Ring to rule them all, One Ring to find them,One Ring to bring them all and in the darkness bind them. • John Ronald Reuel Tolkien • The silence often of pure innocencePersuades when speaking fails. • William Shakespeare • General propositions do not decide concrete cases. • Oliver Wendell Holmes • A philosopher of imposing stature doesn’t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or is not known in the time when he lives. • Alfred North Whitehead
OBJECTIVES In this chapter you will learn: • What polymorphism is, how it makes programming more convenient, and how it makes systems more extensible and maintainable. • To declare and use virtual functions to effect polymorphism. • The distinction between abstract and concrete classes. • To declare pure virtual functions to create abstract classes. • How to use run-time type information (RTTI) with downcasting, dynamic_cast, typeid and type_info. • How C++ implements virtual functions and dynamic binding "under the hood." • How to use virtual destructors to ensure that all appropriate destructors run on an object.
13.1 Introduction • 13.2 Polymorphism Examples • 13.3 Relationships Among Objects in an Inheritance Hierarchy • 13.3.1 Invoking Base-Class Functions from Derived- Class Objects • 13.3.2 Aiming Derived-Class Pointers at Base-Class Objects • 13.3.3 Derived-Class Member-Function Calls via Base- Class Pointers • 13.3.4 Virtual Functions • 13.3.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers • 13.4 Type Fields and switch Statements • 13.5 Abstract Classes and Pure virtual Functions
13.6 Case Study: Payroll System Using Polymorphism • 13.6.1 Creating Abstract Base Class Employee • 13.6.2 Creating Concrete Derived Class SalariedEmployee • 13.6.3 Creating Concrete Derived Class HourlyEmployee • 13.6.4 Creating Concrete Derived Class CommissionEmployee • 13.6.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee • 13.6.6 Demonstrating Polymorphic Processing • 13.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding "Under the Hood" • 13.8 Case Study: Payroll System Using Polymorphism and Run-Time Type Information with Downcasting, dynamic_cast, typeid and type_info • 13.9 Virtual Destructors • 13.10 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System • 13.11 Wrap-Up
13.1 Introduction • Polymorphism with inheritance hierarchies • “Program in the general” vs. “program in the specific” • Process objects of classes that are part of the same hierarchy as if they are all objects of the base class • Each object performs the correct tasks for that object’s type • Different actions occur depending on the type of object • New classes can be added with little or not modification to existing code
13.1 Introduction (Cont.) • Example: Animal hierarchy • Animal base class – every derived class has function move • Different animal objects maintained as a vector of Animal pointers • Program issues same message (move) to each animal generically • Proper function gets called • A Fish will move by swimming • A Frog will move by jumping • A Bird will move by flying
13.2 Polymorphism Examples • Polymorphism occurs when a program invokes a virtual function through a base-class pointer or reference • C++ dynamically chooses the correct function for the class from which the object was instantiated • Example: SpaceObjects • Video game manipulates objects of types that inherit from SpaceObject, which contains member function draw • Function draw implemented differently for the different classes • Screen-manager program maintains a container of SpaceObject pointers • Call draw on each object using SpaceObject pointers • Proper draw function is called based on object’s type • A new class derived from SpaceObject can be added without affecting the screen manager
Software Engineering Observation 13.1 • With virtual functions and polymorphism, you can deal in generalities and let the execution-time environment concern itself with the specifics. You can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types (as long as those objects belong to the same inheritance hierarchy and are being accessed off a common base-class pointer).
Software Engineering Observation 13.2 • Polymorphism promotes extensibility: Software written to invoke polymorphic behavior is written independently of the types of the objects to which messages are sent. Thus, new types of objects that can respond to existing messages can be incorporated into such a system without modifying the base system. Only client code that instantiates new objects must be modified to accommodate new types.
13.3 Relationships Among Objects in an Inheritance Hierarchy • Demonstration • Invoking base-class functions from derived-class objects • Aiming derived-class pointers at base-class objects • Derived-class member-function calls via base-class pointers • Demonstrating polymorphism using virtual functions • Base-class pointers aimed at derived-class objects • Key concept • An object of a derived class can be treated as an object of its base class
13.3.1 Invoking Base-Class Functions from Derived-Class Objects • Aim base-class pointer at base-class object • Invoke base-class functionality • Aim derived-class pointer at derived-class object • Invoke derived-class functionality • Aim base-class pointer at derived-class object • Because derived-class object is an object of base class • Invoke base-class functionality • Invoked functionality depends on type of the handle used to invoke the function, not type of the object to which the handle points • virtual functions • Make it possible to invoke the object type’s functionality, rather than invoke the handle type’s functionality • Crucial to implementing polymorphic behavior
Outline CommissionEmployee.h (1 of 2)
Outline Function earnings will be redefined in derived classes to calculate the employee’s earnings CommissionEmployee.h (2 of 2) Function print will be redefined in derived class to print the employee’s information
Outline CommissionEmployee.cpp (1 of 4)
Outline CommissionEmployee.cpp (2 of 4)
Outline CommissionEmployee.cpp (3 of 4) Calculate earnings based on commission rate and gross sales
Outline CommissionEmployee.cpp (4 of 4) Display name, social security number, gross sales and commission rate
Outline BasePlusCommissionEmployee.h (1 of 1) Redefine functions earnings and print
Outline BasePlusCommissionEmployee.cpp (1 of 2)
Outline BasePlusCommissionEmployee.cpp (2 of 2) Redefined earnings function incorporates base salary Redefined print function displays additional BasePlusCommissionEmployee details
Outline fig13_05.cpp (1 of 5)
Outline fig13_05.cpp (2 of 5) Aiming base-class pointer at base-class object and invoking base-class functionality
Outline fig13_05.cpp (3 of 5) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking base-class functionality
Outline fig13_05.cpp (4 of 5)
Outline fig13_05.cpp (5 of 5)
13.3.2 Aiming Derived-Class Pointers at Base-Class Objects • Aim a derived-class pointer at a base-class object • C++ compiler generates error • CommissionEmployee (base-class object) is not a BasePlusCommissionEmployee (derived-class object) • If this were to be allowed, programmer could then attempt to access derived-class members which do not exist • Could modify memory being used for other data
Outline fig13_06.cpp (1 of 2) Cannot assign base-class object to derived-class pointer because is-a relationship does not apply
Outline fig13_06.cpp (2 of 2)
13.3.3 Derived-Class Member-Function Calls via Base-Class Pointers • Aiming base-class pointer at derived-class object • Calling functions that exist in base class causes base-class functionality to be invoked • Calling functions that do not exist in base class (may exist in derived class) will result in error • Derived-class members cannot be accessed from base-class pointers • However, they can be accomplished using downcasting (Section 13.8)
Outline fig13_07.cpp (1 of 2) Cannot invoke derived-class-only members from base-class pointer
Outline fig13_07.cpp (2 of 2)
Software Engineering Observation 13.3 • If the address of a derived-class object has been assigned to a pointer of one of its direct or indirect base classes, it is acceptable to cast that base-class pointer back to a pointer of the derived-class type. In fact, this must be done to send that derived-class object messages that do not appear in the base class.
13.3.4 Virtual Functions • Which class’s function to invoke • Normally • Handle determines which class’s functionality to invoke • With virtual functions • Type of the object being pointed to, not type of the handle, determines which version of a virtual function to invoke • Allows program to dynamically (at runtime rather than compile time) determine which function to use • Called dynamic binding or late binding
13.3.4 Virtual Functions (Cont.) • virtual functions • Declared by preceding the function’s prototype with the keyword virtual in base class • Derived classes override function as appropriate • Once declared virtual, a function remains virtual all the way down the hierarchy • Static binding • When calling a virtual function using specific object with dot operator, function invocation resolved at compile time • Dynamic binding • Dynamic binding occurs only off pointer and reference handles
Software Engineering Observation 13.4 • Once a function is declared virtual, it remains virtual all the way down the inheritance hierarchy from that point, even if that function is not explicitly declared virtual when a class overrides it.
Good Programming Practice 13.1 • Even though certain functions are implicitly virtual because of a declaration made higher in the class hierarchy, explicitly declare these functions virtual at every level of the hierarchy to promote program clarity.
Error-Prevention Tip 13.1 • When a programmer browses a class hierarchy to locate a class to reuse, it is possible that a function in that class will exhibit virtual function behavior even though it is not explicitly declared virtual. This happens when the class inherits a virtual function from its base class, and it can lead to subtle logic errors. Such errors can be avoided by explicitly declaring all virtual functions virtual throughout the inheritance hierarchy.
Software Engineering Observation 13.5 • When a derived class chooses not to override a virtual function from its base class, the derived class simply inherits its base class’s virtual function implementation.
Outline CommissionEmployee.h (1 of 2)
Outline CommissionEmployee.h (2 of 2) Declaring earnings and print as virtual allows them to be overridden, not redefined
Outline BasePlusCommissionEmployee.h (1 of 1) Functions earnings and print are already virtual – good practice to declare virtualeven when overriding function
Outline fig13_10.cpp (1 of 5)
Outline fig13_10.cpp (2 of 5) Aiming base-class pointer at base-class object and invoking base-class functionality
Outline fig13_10.cpp (3 of 5) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking derived-class functionality via polymorphism and virtual functions
Outline fig13_10.cpp (4 of 5)
Outline fig13_10.cpp (5 of 5)
13.3.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers • Four ways to aim base-class and derived-class pointers at base-class and derived-class objects • Aiming a base-class pointer at a base-class object • Is straightforward • Aiming a derived-class pointer at a derived-class object • Is straightforward • Aiming a base-class pointer at a derived-class object • Is safe, but can be used to invoke only member functions that base-class declares (unless downcasting is used) • Can achieve polymorphism with virtual functions • Aiming a derived-class pointer at a base-class object • Generates a compilation error
Common Programming Error 13.1 • After aiming a base-class pointer at a derived-class object, attempting to reference derived-class-only members with the base-class pointer is a compilation error.
Common Programming Error 13.2 • Treating a base-class object as a derived-class object can cause errors.