Virtual Functions In C++

Virtual function is a member function of a base class and redefined by a derived class.
You need to make use of the keyword virtual, to create a virtual function. The redefined function in the derived class is done to meet the needs of the derived class.
The main advantage of virtual function is that they support run-time polymorphism. Check out the example:
Out put of the program :
In the starting We’ve created three classes: base (which is our base class), der1 and der2 (these two are derived from base). The function made virtual is the display ( ) function.
Notice that We use the virtual keyword only in the base’s virtual function. The derived classes der1 and der2, redefine the display ( ) function.
Now come to the main ( ) function.
base *p; As you know this is how a pointer is created. Instead of creating a pointer to an integer (or character as we usually do), we’re creating a pointer to point to base (which is a class). Hence you can say that p is pointing to an object of type base.
A pointer which points to an object of the base class, can point to an object of a derived class. But the reverse is not possible.
Next comes: p = &b; This assigns the address of the object b (which is an object of base) to the pointer p. Remember that p is a pointer to base type and b is an object of base type. So there’s no problem.
Pointers can be used to invoke member functions. Invoking in this way is done by using the following operator: -> (it’s a minus followed by greater than sign). Since p is pointing to an object of base, the display( ) function of base will be executed.
Next comes: p = &d1; A pointer to object of base can point to an object of a derived class. d1 is an object of der1 (which is derived from base).
Therefore, p can point to d1 and the address of d1 is now assigned to p. When the display ( ) function is now invoked using the pointer p, the compiler will run the derived class der1’s version of display ( ).
This decision of choosing which function to execute is made at run-time. Hence this is known as run-time polymorphism.
Pure Virtual Function
A while back someone asked me how to solve a particular problem, one where there are objects of two different class types, with a common member function interface, and a desire for the member functions to know which object type they are called with.
One simple way of solving this problem looks like this:
Out put of the program :
Class1 and Class2 have no derivation or inheritance relationship with each other.
Using a base class pointer, we can call either f() member function, and up in the right place, with the f() that is called knowing what type of object it has been called on.
In this simple example, Base is an abstract class, and Base::f() a pure virtual function (denoted by “= 0”). A class is abstract if it contains at least one pure virtual function.
No instances of an abstract class may be created, nor may an abstract class be used as a function argument or return type. Abstract class pointers and references are always legal.
Typically an abstract class is used just as in the example above, to serve as a base and allow for manipulation via pointers or references that actually refer to instances of derived types.