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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #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 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

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.

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #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; } |