0% found this document useful (0 votes)
6 views9 pages

Understanding C++ Virtual Functions

Uploaded by

G.satheesh Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Understanding C++ Virtual Functions

Uploaded by

G.satheesh Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

virtual

Virtual base class

The virtual base classes are used to avoid


duplication of data.

When the class parent is made a virtual base


class, care is taken by the compiler to see that only,
one copy of the base class parent is inherited

2
Virtual functions

Virtual functions are the C++ language's feature to


achieve the run-time polymorphism.

Base class pointer can point to derived class


object. In this case, using base class pointer if we
call some function which is in both classes, then
base class function is invoked. But if we want to
invoke derived class function using base class
pointer, it can be achieved by defining the function
as virtual in base class, this is how virtual functions
support runtime polymorphism

3
Virtual function is a member function that is
declared within a base class and redefined by a
derived class to suit its own needs.
When a function is made virtual, the decision
as to which function to be invoked is done at the
run-time based on the type of the object pointed to
by the pointer, rather than the type of the pointer.
Virtual table

To implement virtual functions, C++ uses a


special form of late binding known as the virtual
table created in compile time.

The virtual table is a lookup table of addresses


of only the virtual functions in the corresponding
class to resolve the functions with the proper
function calls
Rules of virtual functions

1. The virtual functions must be non-static members.


2. The virtual functions are accessed by using
pointers to objects.
3. Constructor cannot be made virtual, but
destructors can be made.
4. Virtual function defined in the base class need not
be necessarily redefined in the derived class. In
such scenario, calls will invoke the base function.
Rules of virtual functions(contd…)

5. A base class pointer can refer to the derived class


objects, but the reverse is not true.
6 . Virtual functions should be declared in the public
section of a class.
Pure virtual functions

A virtual function that is declared but not defined in


a base class is referred to as a pure virtual function.

Syntax:-
virtual <return-type> <function_name>(arg...) = 0;
Abstract class

If a class contains at least one pure virtual


function is called as Abstract class.
If a virtual function declared as pure, it must be
redefined in each derived class, else compilation of
the program is unsuccessful.

Note:-A class having pure virtual function cannot be


used to instantiate object of its own.

You might also like