Learn Virtual Function in C++ with Real-time Example

Free C++ course with real-time projects Start Now!!

It is time to dive deep into the core concepts of C++, which makes it distinguishable from other programming languages. After completing this tutorial, you would develop a precise knowledge of the virtual function C++ that allows you to perform operations with classes and objects that you never knew existed.

Before we begin, it is important to note that a thorough knowledge of Classes and Objects in C++ is required to understand this tutorial.

We are going to discuss the following topics here to get it a better way-

Virtual Function in C++ with Example

What is Virtual Function in C++?

Continuing our discussion from where we left off in Polymorphism in C++. Now, it’s time to further elaborate on our discussion on Virtual functions in C++. It is important to remember that virtual function plays a vital role in achieving runtime polymorphism.

A virtual function is nothing but a member function of a base class that you redefine in a derived class. The property of redefining a member function declared in the base class to a derived class is also called Function Overriding.

If you want to call a virtual function in C++ and execute the redefined version of the member function, you can achieve this task using a pointer (*pointer_name) or a reference (->) by referring a derived class object to the base class.

A base class pointer has the capability to point to the objects of the base class and the derived class.

We can declare a member function of the base class as a virtual function with the help of the “virtual” keyword.

Rules for Virtual Function in C++

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Here are some of the points you need to keep in mind while working with virtual functions in C++:

  1. Since we already specify the member function to be a virtual function in the base class with the help of the “virtual” keyword. Now we need not again specify it while defining it in the derived class.
  2. A member function of the base class cannot be a virtual function and a friend function simultaneously.
  3. A member function of the base class cannot be a virtual function with the keyword static.
  4. We can access virtual functions only with the help of a pointer or a reference.
  5. The basic model of the virtual function in the base class should be similar while redefining it in the derived class.
  6. It is not necessary to redefine the virtual function in the derived class. If the virtual function is not overridden, then we can use the original function declared or defined in the base class.
  7. It is not possible to introduce a virtual constructor inside the class, although it is possible to introduce a virtual destructor.

Virtual Function in C++ Significance

Now, the question arises: What is the significance of virtual functions?

So you’re not allowed to worry about this. In order to answer this question, you need to understand that virtual functions ensure that, the correct function is called for an object at the time of the function call. It as simple as it is looking, there’s nothing complicated.

Virtual Function in C++ with Example

Let us understand how we can implement a virtual function with the help of a C++ program:

#include <iostream> 
using namespace std;

class A 
{ 
// private by deafult
string data = "Data"; // Original data defined in the base class
public: 
virtual void display() // Virtual function defined in the base class
{ 
cout<<"The data is : "<<data<<endl; 
} 
}; 
class B: public A 
{ 
string value = "DataFlair"; // Virtual function redefined in the derived class
public: 
void display() 
{ 
cout << "The data is: "<<value<<endl; 
} 
}; 
int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

A *a; 
B b; 
a = &b; 
a-> display(); // Changes reflected in the original data by function call
return 0; 
}

Code-

Example of Virtual Function in C++

Output-

Output of Virtual Function in C++

Here, we infer that changes made in the original function defined in the base class are reflected in the derived class with the help of virtual function.

Quiz on Virtual Function in C++

Summary

Its been a great journey up till here. Although it was short, still it was informative and better to grasp the concept. We covered a very important topic in a very enjoyable way. It’s been difficult for an individual now to forget the concept. We learned each and every impact of virtual functions in C++. We also studied its rules, significance and tried to understand it with an example.

Get ready for the next article – Inline Function in C++

After reading the article if any individual still finds any problem then s/he can comment in the comment section below. We will try our best to serve you.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. Girijala.Mounika says:

    i cant get real lifetime use of virtual function

Leave a Reply

Your email address will not be published. Required fields are marked *