Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

VIRTUAL FUNCTIONS

AND
ABSTRACT CLASSES
VIRTUAL FUNCTIONS

• A virtual function is a member function which


is declared within a base class and is re-
defined (Overridden) by a derived class.

• When you refer to a derived class object


using a pointer or a reference to the base
class you can call a virtual function for that
object and execute the derived class`s
version of the function.
VIRTUAL FUNCTIONS

● Virtual functions ensure that the correct function is called for


an object, regardless of the type of reference (or pointer) used
for function call.
● They are mainly used to achieve Runtime polymorphism.
● Functions are declared with a virtual keyword in base class.
● The resolving of function call is done at Run-time.
RULES FOR VIRTUAL FUNCTIONS

● Virtual functions cannot be static and also cannot be a friend


function of another class.
● Virtual functions should be accessed using pointer or
reference of base class type to achieve run time
polymorphism.
● The prototype of virtual functions should be same in base as
well as derived class.
RULES FOR VIRTUAL FUNCTIONS

● They are always defined in base class and overridden in


derived class. It is not mandatory for derived class to override
(or re-define the virtual function), in that case base class
version of function is used.
● A class may have virtual destructor but it cannot have a virtual
constructor.
NOTE: If we have created a virtual function in the base class
and it is being overridden in the derived class then we don`t
need virtual keyword in the derived class, functions are
automatically considered as virtual functions in the derived
class.
Working of virtual functions
(concept of VTABLE and VPTR)
If a class contains a virtual function then compiler itself
does two things:

● If object of that class is created then a virtual pointer(VPTR) is


inserted as a data member of the class to point to VTABLE of
that class. For each new object created, a new virtual pointer
is inserted as a data member of that class.
Working of virtual functions
(concept of VTABLE and VPTR)

● Irrespective of object is created or not, a static array of


function pointer called VTABLE where each cell contains the
address of each virtual function contained in that class.
ABSTRACT CLASSES

• Abstract class is a class which contains at


least one Pure Virtual function.

• Abstract classes are used to provide an


Interface for its sub classes.

• Classes inheriting an Abstract Class must


provide definition to the pure virtual function,
otherwise they will also became abstract
class.
CHARACTERISTICS OF
ABSTRACT CLASS
● Abstract class cannot be instantiated, but pointers and
references of Abstract class type can be created.
● Abstract class can have normal functions and variables along
with a pure virtual function.
● Abstract classes are mainly used for Up casting, so that its
derived classes can use its interface.
● Classes inheriting an Abstract Class must implement all pure
virtual functions, or else they will become Abstract too.
PURE VIRTUAL FUNCTIONS IN C++

● Pure virtual Functions are virtual functions with no definition.


● They start with virtual keyword and ends with = 0. Here is the
syntax for a pure virtual function.

Virtual void f() = 0;


Why can`t we create Object of an
Abstract Class?

● When we create a pure virtual function in Abstract class, we


reserve a slot for a function in the VTABLE, but doesn`t put
any address in that slot. Hence the VTABLE will be
incomplete.
Why can`t we create Object of an
Abstract Class?

● As the VTABLE for Abstract class is incomplete, hence the


compiler will not let the creation of object for such class and
will display an error message whenever you try to do so.
PURE VIRTUAL DEFINITION

Pure Virtual functions can be given a small definition in the


Abstract class which you want all the derived classes to have.
Still you cannot create object of Abstract class.

Also, the Pure Virtual function must be defined outside the class
definition. If you will define it inside the class definition, complier
will give an error. Inline pure virtual definition is illegal.

You might also like