Virtual Function in C++

In this tutorial, we will learn Virtual Function in C++.

Virtual programming is an innovation to the object-oriented programming language. Why so? And what are the advantages and necessity of this? We will learn in this tutorial.

A virtual function is a member function in the base class that you expect to be overridden or redefined in the child class. Actually, it tells the compiler for late binding about the current function. The main advantage is that it directly supports the OOPS concept. Here we use the concept of late binding(which is a method being called upon an object or the function being called with arguments is looked up by name at runtime). On using this virtual keyword C++ creates a dummy virtual table(VTABLE) which is a table that looks for the virtual functions which always be consulted upon calling this.

There are certain rules for using this concept. Those are:

  1. This function must be members of some class.
  2. Remember one thing definition of this function must be inside a base class.
  3. Always remember these functions should not be static members.
  4. For accessing virtual function we need an object pointer.
  5.  This function can be a friend to another class.
  6. There is no virtual constructor. But fortunately, we have virtual destructor.
  7. If the declaration is done inside a class, then it should be declared as public, which is required.

Also learn:

    Find the difference between two numbers using virtual
function within a class


Let’s have a snippet of a program with a class below. Then we will discuss the program later on.

using namespace std;
int a,b;//Globally declared
class parent
  virtual void differ(int a,int b)=0;
  //or else
  // virtual void differ(int a,int b){}
class child:public parent
   void differ(int a,int b)
     cout<<"The Difference is: "<<(a-b);

int main()
   parent *p;
   child c;
   cout<<"Take two numbers as output: "<<endl;
   return 0;


The Difference is 4



We have the example above. Let’s explore it.

  1.   First, we have declared two variable as


int a,b; // Globally declared

to access the two variable globally inside the code;


2. Now we will look forward. On the next step, we have declared two classes.

First one is parent class

The second one is the child class.

3. Previously we have told that a parent class should have a declaration of the virtual function and most importantly that declaration should be a public one. There are two formats of declaration:

virtual void differ(int a,int b)=0;
//virtual void differ(int a,int b){}

Above declaration inside the parent class should be public. We have mentioned two types of declaration:

Format is like:-

virtual‘  + void+ function(parameters) =0;

or else         ‘virtual‘ + void + function(parameter){}   

[Remember: the second declaration  should not have any ‘;’ at the end]

4. As per the rules, declaration should be inside parent class. But most importantly definition should be inside the child class.

So in example child class which is inheriting parent class,


class child:public parent

should contain the definition of the virtual function.

virtual void differ(int a,int b)
  cout<<"The Difference is "<<(a-b);

5. Now the main part which is very important in virtual function.

Initially, we create a pointer of type base class and initialize it with the address of the derived class object. When we create an object of the derived class, the compiler creates a pointer as a data member of the class containing the address of VTABLE of the derived class.

It’s looking complicated right? Let’s simplify this :


parent *p;
child c;

Here we have created a pointer of the base class, ‘parent‘ and initialize it to the address of derived class, ‘child‘ with the help of the objects of both classes.


6.  On the next step we have codes something like:

// This is the part of late binding

In the above part, we are clearly pointing to the defined function inside child class through the base class.


This is all about the simple basic concept of the Virtual function in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *