What is Virtual Functions in C++?
Virtual functions are very useful when one tries to access an overridden member function of a child class which is also present in the parent class using a child class object pointed by the parent class pointer.
Why use virtual functions?
When you perform function overriding and try to access the child class member function using the child class object which is pointed by the parent class pointer, the parent class member function is executed instead of the function present in the child class member function.
Let’s check this out by the code
Code:
C++ Code
#include <iostream>
using namespace std;
class Parent {
public:
void print() {
cout << "Parent Class Function" << "\n";
}
};
class Child: public Parent {
public: void print() {
cout << "Child Class Function" << "\n";
}
};
int main() {
Child c;
Parent * p = & c;
p -> print();
}
Output: Parent Class Function
Here, we create a child class object c and a parent class pointer p which points to the child class object. Later we try to access the print() function using p. We observe that the parent class member function is executed here. The following output is displayed.
This overruled the concept of function overriding since we studied that function invocation and execution totally depend on the object accessing it.
To correct this, we can use the virtual function which can be seen in the following code
Code:
C++ Code
#include <iostream>
using namespace std;
class Parent {
public:
virtual void print() {
cout << "Parent Class Function" << "\n";
}
};
class Child: public Parent {
public: void print() {
cout << "Child Class Function" << "\n";
}
};
int main() {
Child c;
Parent * p = & c;
p -> print();
}
output: Child Class Function
Rules for virtual functions
- We can also use them to create virtual destructors but no virtual constructor is possible because when a constructor is created no virtual table is created and no virtual pointer is defined.
- Virtual functions can’t be static.
- Since virtual functions are used to perform runtime polymorphism, they should be accessed using pointers of respective classes.
Virtual Table
A class contains a static array of member function pointers known as Vtable or virtual table which store the address of each virtual member function present in that class.
For example, a class C1 contains 3 virtual functions and another inherited class C2 contains 3 override functions. The Vtable would be as follows
Virtual Pointer
When an object of a class is created, a virtual pointer is inserted to point to the virtual table of that class. Every object creation creates a virtual pointer that is pointing to the virtual table of that class.
Disadvantages of Virtual Functions
- They are a bit slow when compared on a large scale i.e. when a large number of objects are created.
- They can be harder to debug since it gets confusing to figure out which function is executed.
- Every time a virtual function is declared in a class, a virtual table is created for that class which increases the space.
Using virtual functions is a good option but sometimes it may also slow down the application. It’s a fair choice to use virtual functions when necessary.
Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article