Virtual Function and Polymorphism
Virtual Function and Polymorphism
Polymorphism means one entity behave different way in different circumstance and it changes its
behavior depends on different situation.
There are two levels at which polymorphism can be achieved. They are
1. Compile-time polymorphism
2. Run-time polymorphism
Run time polymorphism is achieved through “virtual functions”. Here binding or linking of a
function definition to a specific function call is done during run time. Hence polymorphism
achieved through “virtual functions” is known as run-time polymorphism. Dynamic
binding requires the use of pointers to object.
Polymorphism
class A
{
public:
void display() // overridden function
{
cout<<"Manjeet loves India";
}
};
class B: public A
{
public:
void display() // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};
main()
{
B b; //derived class object
return 0;
}
class A
{
public:
void display() // overridden function
{
cout<<"Manjeet loves India";
}
};
class B: public A
{
public:
void display() // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};
main()
{
A a; //base class object
return 0;
}
Here we notice that, the “display ()” function of the base class is called irrespective of the type of the
object pointed at by the pointer.
This is because the appropriate function to be called is decided by the compiler at the compile time, by
considering the type of pointer, but not the type of object pointed to by the pointer. Hence this is a
serious problem, which has to be taken care of.
#include<iostream>
class A
public:
};
class B: public A
public:
};
main()
return 0;
}
Virtual Function
● Virtual functions are the C++ language’s feature to achieve the run-time polymorphism.
● When we use the same function name in both the base and the derived classes, the function
in base class is declared as virtual using the keyword “virtual” prefixed with the function
declaration.
● 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.
● The keyword virtual is required only for the declaration but not for the definition. Once a function
is declared as virtual in the base class, it is virtual in all the derived classes.
Syntax
Or
Eg:
#include<iostream>
class A
public:
};
class B: public A
public:
};
main()
return 0;
● A base class pointer can refer to the derived class objects, but the reverse is not true.
Or
A virtual function that is declared but not defined in a base class is referred to as a pure virtual
function. The general syntax for declaring a pure virtual function is as follows:
Or
Note: here function is initialized to zero. The assignment operator ‘=’ has nothing to do with the
assignment i.e the value zero is not assigned to anything. It is used to simply inform the compiler that,
the function will be pure and does not have any body.
#include<iostream>
using namespace std;
class A
{
public:
void virtual display( )=0; // virtual function
};
class B: public A
{
public:
void display( ) // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};
main()
{
B b; //derived class object
A *aptr; //Base class pointer
aptr=&b; // base class Pointer pointing to derived class objects
aptr->display(); // calls derived class “display()” function
return 0;
}
“A virtual function which gives an interface for the derived class functions to override but must not
be invoked itself through virtual mechanism is called pure virtual function.”
● When the base class uses a pure virtual function each derived class must override that
function, failing of which a compile time error is generated.
● A class containing a “pure virtual function” is called Abstract class. We cannot create an
object of an Abstract class. We can however declare pointers to it.
● Abstract class is a design concept only to act as a base class upon which other derived classes
are set up.
● If a virtual function is declared as pure, it must be redefined in each derived class, else the
compilation of the program is unsuccessful.
Q: Write a C++ program demonstrating use of the pure virtual function with the use of base and
derived classes.