Site icon T4Tutorials.com

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.

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

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
compiler can access the Virtual Functions through object pointers
the compiler can access the Virtual Functions through object pointers

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.

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.

#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

Exit mobile version