Virtual Functions and Runtime Polymorphism in C++

Points to know before gathering information about virtual function.

Base class pointer can point to the object of any of its descendant class (or derived class) but its converse is not true.


For Example:-
#include<iostream.h>
using namespace std;

class Integer {
 private:
  int num;
  
 public:
  void setData(int n1) {
   num = n1;
  }
 };
 
 class PositiveInteger: public Integer {
  private:
   int num2;
   
  public:
   void setData(int n2) {
    num2 = n2;
   }
  };
  
int main() {
 Integer *I;
 PositiveInteger P;
 I = &P;
 return 0;
}


Here, I is a pointer of base class Integer (I is a base class pointer) and P is an object of derived class PositiveInteger. Since I is a base class pointer, it can point to the derived class object like P in this case. 

It can also point to the further derived class of Integer class or PositiveInteger class object.


Virtual Function in C++

1. Virtual function is that member function of base class which is redefined in derived class.

2. Virtual function is declared with 'virtual' keyword.

3. It is used to tell the compiler to do late binding or dynamic linkage on function.

4. It is used in the case of function overriding for binding done at run time.

5. It is used to achieve runtime polymorphism in C++.

Let us see an example given below.
#include<iostream>
using namespace std;
class A {
 public:
  void Fun1() {
   //...
  }
 };
 
class B: public A {
 public:
  void Fun1()             //Method overriding.
  {
   //...
  }
 };
 
int main() {
A *P;
B O2;

P = &O2;
O2.Fun1();              //Fun1 of class B called.

P->Fun1();           //Fun1 of class A called.
return 0;
}


Here, P is a base class pointer that is why it can point to the object of derived class B (i.e., P points to O2). 

Lets talk about the code P->Fun1(); , C++ compiler did not know where P is pointing to because early binding is done here at compile time, (P do not point to O2 at compile time).

Because P points to O2 at run time when memory allocation is done . That's why, C++ compiler will try to find the correct definition of Fun1 function.

The compiler will consider the type of P (P is of class A). So, it binds the Fun1 function with class A Fun1 function rather than class B Fun1 function.

This is called early binding (binding done at compile time). In order to solve this problem and to do late binding, the concept of virtual function came into picture.

The function needs to make virtual by using 'virtual' keyword before function name.

Let us see the same example with virtual function.
#include<iostream>
using namespace std;

class A {
 public:
  virtual void Fun1()      //virtual function
  {
   //...
  }
 };
 
 class B: public A  {
  public:
   void Fun1()           //Method overriding.
   {
    //...
   }
  };
  
int main() {
A *P;
B O2;

P = &O2;
O2.Fun1();           //Fun1 of class B called.

P->Fun1();        //Fun1 of class B called.
return 0;
}


Fun1 function in derived class B will automatically become virtual by the class A virtual function. Here, late binding is done by the compiler at run-time

So, the compiler will now consider the content that P is pointing (P is pointing to object of class B). So, it binds the Fun1 function with class B. This is the advantages of virtual function in C++.


Working of Virtual Function in C++

1. There is a concept of vtable and vpointer (vptr). vptr and vtable is created and maintained by the compiler itself for the working of late binding and virtual function.

2. On seeing a virtual function inside any class, C++ compiler makes vptr inside the object of that class and vtable is also made by the compiler

3. Vptr is a pointer which points to a static array (or static table) vtable.

4. Vtable is a static table of function pointers in which the addresses of virtual functions are stored.


working-of-virtual-function-in-c++vtable-vptr-learning-mania.jpg


Points to Remember:-

1. In case of early binding, the compiler binds the function call with the function of that class of which the pointer is.

2. In case of late binding, the compiler binds the function call by considering the objects to which the pointer is pointing. Inside the object, there is a vptr (vpointer) which points to the vtable. The compiler binds the function call with that vtable function which points to the virtual function in the class.


What is Pure Virtual Function in C++

1. A do nothing function is called pure virtual function in C++.
2. Pure virtual function is also called abstract function in C++.
3. We do not have implementation of a pure virtual function, we only declare it.
4. A pure virtual function is declared by assigning 0 in its declaration.


For Example:- pure virtual function in c++

#include<iostream>
//an Abstract Class
class AbstractClass {
 //Data Members of AbstractClass
 
 public:
  //Pure virtual function
  void virtual showData() = 0;
 };



<< Previous                                                                                      Next >>
Previous
Next Post »