Virtual Function in C++

A virtual function is a member function of the base class (parent) and a virtual function redefine in a derived class (child). A virtual function is declared by using the virtual keyword.

  • It is used to tell the compiler to perform dynamic linkage or late binding on the function.

Function overriding means to write a function in the child class that is already present in the parent class.

Function overriding means making a function more important than any other function.

In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime and then binds the function call.

Example of C++ program without virtual function

without virtual function
without virtual function
#include <iostream>  
using namespace std;  
class A  
{  
   int n1=33;  
    public:  
     void display()  
    {  
        std::cout << "Value of n1 is : " << n1<<std::endl;  
    }  
};  
class B: public A  
{  
    int n2 = 44;  
    public:  
    void display()  
    {  
        std::cout << "Value of n2 is : " <<n2<< std::endl;  
    }  
};  
int main()  
{  
    A *object1;  
    B object2;  
    object1 = &object2;  
   object1->display();  
    return 0;  
}

Output

Value of n1 is : 33

Example of C++ program using virtual function

Late binding can be implemented by making a function as a virtual function.

C++ program to demonstrate working of virtual function

#include <iostream>  
using namespace std;  
class A  
{  
   int n1=33;  
    public:  
     virtual void display()  
    {  
        std::cout << "Value of n1 is : " << n1<<std::endl;  
    }  
};  
class B: public A  
{  
    int n2 = 44;  
    public:  
    void display()  
    {  
        std::cout << "Value of n2 is : " <<n2<< std::endl;  
    }  
};  
int main()  
{  
    A *object1;  
    B object2;  
    object1 = &object2;  
   object1->display();  
    return 0;  
}

 

Features of Virtual Function

  • Virtual functions must be members of some class.

    Virtual functions must be members of some class
    Virtual functions must be members of some class
  • Virtual functions cannot be static members.

If we try to add virtual functions as static members, then the following error will display;

[Error] member ‘display’ cannot be declared both virtual and static

Virtual functions cannot be static members
Virtual functions cannot be static members
  • The compiler can access the Virtual Functions through object pointers.

compiler can access the Virtual Functions through object pointers
the compiler can access the Virtual Functions through object pointers
  • A virtual function must be defined in the base/parent class, whether it is used or not.

In this example, the display is a virtual function in base class A.

  • The signatures of a virtual function of the base class and all the derived classes must be the same.

The signatures of a virtual function of the base class and all the derived classes must be the same.
The signatures of a virtual function of the base class and all the derived classes must be the same.

Why it must be the same? If two functions with the same name exist but have different signatures, then the compiler will understand these functions as overloaded functions.

  • A virtual destructor is possible but a program cannot have a virtual constructor.

#include<iostream>
using namespace std;
class Parent {
   public:
      Parent() {
         cout<<"Constructor is started for parent class \n";
      }
      virtual ~Parent() {
         cout<<"Destructor is started for parent class \n";
      }
};
class child: public Parent {
   public:
      child() {
         cout<<"Constructor is started for child class \n";
      }
      ~child() {
         cout<<"Destructor is started for Child class \n";
      }
};
int main(void) {
   child *t4tutorials = new child();
   Parent *dotcom = t4tutorials;
   delete dotcom;
   return 0;
}

 

[Examples – Difference] Early binding and Late Binding in C++

Virtual Function MCQs

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.