A virtual function is a member function that is defined in the base class/parent class and re-defined in the derived class. Virtual function allows calling the derived class version of the function using the reference or pointer to the base class.
Let’s see a few examples to understand the concept of virtual function.
In the first example, we will see the behavior of the non-virtual function, and then in the second example, we will explain the behavior of a virtual function.
Example 1
In the following example code, we have two classes: base class and derived class.
Base class has a member function, i.e., display(). The derived class is inherited from the base class and redefined the display() function.
Then, we have declared a pointer to the base class type and assigned an object of the derived class. So, when we call the display() function using the base class type pointer, the base class function will be called. You can see the output below.
But, in such cases, C++ provides a way to call the derived class function by declaring the base class function as virtual. We will see another example to understand this.
using namespace std;
class Base_Class
{
public:
void display()
{
cout << "I am in Base class" << endl;
}
};
class Derived_Class:public Base_Class
{
public:
void display()
{
cout << "I am in Derived class" <display();
return 0;
}
Example 2
In this example, we have defined the display() function as a virtual function in the base class. The only difference from the previous program is that we have added a virtual keyword in front of “void display()” in the base class.
Now, if you see the output, it prints, “I am in derived class”, which indicates that the derived class function is called.
The virtual keyword (virtual function) in the base class is responsible to ensure that the right function is called for an object.
using namespace std;
class Base_Class
{
public:
virtual void display()
{
cout << "I am in Base class" << endl;
}
};
class Derived_Class:public Base_Class
{
public:
void display()
{
cout << "I am in Derived class" <display();
return 0;
}
Example 3
This is another example of a virtual function. As you can see in the below program, we have defined a base class, i.e., Animal. There are two derived classes: Dog and Cow. We have defined the eat() function as virtual in the base class, i.e., Animal. We have then redefined the eat() function in both the derived classes, Dog and Cow. In the main() function, we have a pointer of the base class, i.e., Animal, and then attached the derived class, Dog. So, when we call the eat() function using the base class pointer, we can invoke the derived class version of eat() function, i.e., the eat() function from the Dog class. Similarly, when we attach the Cow class object, we can then invoke the derived class version of the eat() function, i.e., the eat() function from the Cow class. You can clearly see this behavior in the output below.
using namespace std;
class Animal
{
public:
virtual void eat()
{
cout << "Animal - base class - undefined eating behavior." << endl;
}
};
class Dog:public Animal
{
public:
void eat()
{
cout << "Dog - eat non-veg!" << endl;
}
};
class Cow:public Animal
{
public:
void eat()
{
cout << "Cow - eat veg!" <eat();
a_ptr = new Cow();
a_ptr->eat();
return 0;
}
Conclusion
In this article, I have explained the concept of virtual function in C++. The C++ supports different types of polymorphism – static polymorphism and dynamic polymorphism. With the help of a virtual function, we can achieve the runtime/dynamic polymorphism. In this article, we have only looked at the concept of virtual function and how to achieve runtime polymorphism. I have explained three working examples to explain virtual function.