What is a Virtual Function and Why You Need it in C++

Spread the love

The word ” virtual ” itself means which does not exist in reality and it kind of imaginary. So, a virtual function means a kind of function which does not exist in reality. With this explanation it is really hard to get a concept about virtual function. As you are searching for the answer about virtual function, I am sure that you are a C++ beginner and you are studying “Polymorphism”.  To get a clear concept about “Virtual Function”, let us consider one example and I believe that after discussing the examples you will get a clear concept about virtual function in C++.

Example to Discuss Virtual Function:

#include <iostream>

using namespace std;

class techntechie {
         public:
         void show(){
         cout<< "Class B method Show"<<endl;

         }

         void display(){
         cout<< "Class B method Display"<<endl;

         }
};

class D: public techntechie {

        public:
        void show(){
        cout<< "Class D method Show"<<endl;

        }

        void display(){
       cout<< "Class D method Display"<<endl;

       }
};

int main (){

techntechie b;
D d;
techntechie *ptr;
ptr = &b;
ptr ->show();
ptr ->display();

ptr = &d;
ptr->show();
ptr->display();
return 0;

}

From the above example, you can observe that my base class is "techntechie" and my derived class is D. In the base class, I have declared two functions named "show" and "display" and the return type is void for both the functions. In the derived class also, I have created two functions with the same name. But the purpose of the functions in the derived class are different than base class (you can check that my show and display functions in the derived class are printing out different things). So, now the question comes how my compiler will know that which function is from which class as they are having the same name.  To direct my compiler to the right functions in both base and derived class, in the main function part, we have declared one pointer named *ptr and this and just after that to that pointer “ptr” we have assigned the address of base class object and pointed the pointer to the base class functions to let the compiler know that it should identify the function from the base class.

On the other hand, we have created another derived class object named “d” and also assigned the address of d to the pointer and also instructed the pointer by the line ptr->show(); that it should point to the function of derived class.

But when we will run the function, the out put will be like below:

Class B method Show
Class B method Display
Class B method Show
Class B method Display

But we expected that the output will be like:

Class B method Show
Class B method Display
Class D method Show
Class D method Display

Then why it is happening? The answer is, it is happening because of early binding of the program. The early binding is also know as compile time binding. Early binding means that when you are compiling the program, it is determining everything. So, when the program finds in the base class that it has two functions with the name “show” and “display”, it executes only this two functions and do not even go to the derived class. So, we only get the output from the base class.

So, what is the solution of this problem or how can we get our desired output? The answer is, we need to use one keyword named “virtual” before the base class functions name. When we will use the keyword “virtual” in front of the function name, late binding will happen. Late binding means binding during the run time of the program. So, it will not bind during compilation time rather the the respective functions will get called during the compile time the correct functions will get called from the program. So, to get the desired output just name the functions (on 5th and 8th line of the code) of the base class as below:

virtual void show(){  

virtual void display(){

Now, you will be able to get the desired output.

So, if we want to summarize the whole thing, the summary is that virtual function  is a function which uses the keyword virtual and when the function uses the keyword late binding happens (run time binding) and the program can differentiate between the same named function of base class and derived class. So, when we want to use polymorphism (same function name with different purpose) in C++, just by using virtual function  we can get desired output.

Himadri Subrah Saha

Himadri is an ICT Professional who writes for his technology tips & tricks related blog TechnTechie. Though it is hard to balance time in between professional life and blogging, he still manages time to work for his own blog and writes almost regularly. The dashboard of this WordPress is the only place where he does not feel tired! Read my other blogs @ PetCare and Teleinfo

0 0 votes
Article Rating

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x