Virtual Functions
Virtual Functions
Contents
• Pointers to derived classes
• Applying polymorphism
• Exercises
Pointers to Derived Classes
• A pointer declared as a pointer to a base class can also be used to point to any class
derived from that base.
• Although you can use a base pointer to point to a derived object, you can access only
those member of the derived object that were inherited from the base.
• This is because the base pointer has knowledge only of the base class.
• While it is permissible for a base pointer to point to a derived object, the reverse is not
true.
• If you point a base pointer to a derived object and then increment that pointer, it will
not be pointing to the next derived object. It will be pointing to the next base object.
Introduction of Virtual Functions
• A virtual function is a member function that is declared within a base class
and redefined by a derived class.
• Virtual functions must be class members. This is not the case for overloaded
functions.
• To ensure that this will occur, C++ supports pure virtual functions.
• A pure virtual function has no definition relative to the base class. Only the function’s
prototype is included. To make a pure virtual function, use this general form:
• When a virtual function is made pure, it forces any derived class to override it.
• It is, technically, an incomplete type, and no objects of that class can be created. Thus,
abstract classes exist only to be inherited.
• However, you can still create a pointer to an abstract class, since it is through the use of
base class pointers that run-time polymorphism is achieved.
• When a derived class inherits a virtual function from a base class and then the derived is
used as a base for yet another derived class, the virtual function can be overridden by the
final derived class.
Applying Polymorphism
• In polymorphism, a single, well-defined interface is used to access a number
of different but related actions, and artificial complexity is removed.
• When related actions are accessed through common interface, you have less
to remember.
Early Binding and Late Binding
• Early binding essentially refers to those events that can be known at compile time.
• Early bound entities include “normal” functions, overloaded functions, and nonvirtual
member and friend functions.
• The main advantage of early binding is that it is very efficient. Calls to functions bound
at compile time are the fastest types of function calls.
• A late bound function call is one in which the address of the function to be called is not
known until the program runs.
• The disadvantage is that there is more overhead associated with a function call.
Exercises
• Create a Shape base class with a virtual function calculateArea(). Derive classes like Circle
and Rectangle, each implementing the calculateArea() function to compute the area of their
respective shapes.
• Define an Employee base class with a virtual function calculateSalary(). Derive classes like
Manager and Engineer, each providing their own implementation of calculateSalary() to
calculate the salary based on their roles.
• Create a BankAccount base class with virtual functions for deposit and withdrawal. Derive
classes like SavingsAccount and CheckingAccount, each implementing these functions
differently. Demonstrate polymorphism by depositing and withdrawing funds from both
types of accounts using a common interface.