0% found this document useful (0 votes)
9 views

Virtual Functions

The document discusses virtual functions in C++, explaining their role in achieving run-time polymorphism through base class pointers pointing to derived class objects. It covers the concepts of early and late binding, the distinction between function overloading and overriding, and introduces pure virtual functions and abstract classes. Additionally, it provides exercises to apply these concepts in practical scenarios.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Virtual Functions

The document discusses virtual functions in C++, explaining their role in achieving run-time polymorphism through base class pointers pointing to derived class objects. It covers the concepts of early and late binding, the distinction between function overloading and overriding, and introduces pure virtual functions and abstract classes. Additionally, it provides exercises to apply these concepts in practical scenarios.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Virtual Functions

Contents
• Pointers to derived classes

• Introduction to virtual functions

• More about virtual functions

• Applying polymorphism

• Early binding and late binding

• 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.

• To create a virtual function, precede the function’s declaration with the


keyword virtual.

• When a class containing a virtual function is inherited, the derived class


redefines the virtual function relative to the derived class.

• Virtual functions implement the “one interface, multiple methods”


philosophy that underlines polymorphism.
Introduction of Virtual Functions
• A virtual function is capable of supporting run-time polymorphism, when a
virtual function is called through pointer.

• When a base pointer points to a derived object that contains a virtual


function and that virtual function is called through that pointer, C++
determines which version of that function will be executed based upon the
type of object being pointed to by the pointer.

• And, this determination is made at run time.

• This process is the way that run-time polymorphism is achieved.


Introduction of Virtual Functions
• The redefinition of virtual function and function overloading are distinctly
different.

• An overloaded function must differ in type and/or number of parameters, while a


redefined virtual function must have precisely the same type and number of
parameters and the same return type.

• Virtual functions must be class members. This is not the case for overloaded
functions.

• While destructor functions can be virtual, constructors cannot.

• The term overriding is used to describe virtual function redefinition.


More About Virtual Functions
• When there is no meaningful action for a base class virtual function to perform, the
implication is that any derived class must override this function.

• 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:

virtual type function-name(parameter-list) = 0

• When a virtual function is made pure, it forces any derived class to override it.

• If a derived class does not, a compile-time error results.


More About Virtual Functions
• When a class contains at least one pure virtual function, it is referred to as an abstract
class.

• 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.

• Polymorphism allows the logical relationship of similar actions to become


apparent, thus, the program is easier to understand and maintain.

• 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.

• The main disadvantage is lack of flexibility.


Early Binding and Late Binding
• Late binding refers to events that must occur at run time.

• A late bound function call is one in which the address of the function to be called is not
known until the program runs.

• In C++, a virtual function is a late bound object.

• The main advantage of late binding is flexibility at run time.

• 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.

You might also like