Virtual Functions and Polymorphism in C++, Virtual Function and Polymorphism are important concepts in C++ that support Object-Oriented Programming. Polymorphism means one function having many forms, allowing the same function name to perform different tasks. In C++, polymorphism can be achieved at compile time (function overloading and operator overloading) and run time using virtual functions.
Virtual Functions and Polymorphism in C++
In C++, virtual functions are the special member functions with the ‘virtual’ keyword which enable runtime polymorphism. It means that the function defined in a base class can be overridden (changed) by the derived class. When the function is called, the virtual function ensures that the correct derived class function will be called. Virtual functions provide flexibility and allow different classes to define their own behaviour while sharing a common interface.
- The keyword ‘virtual’ is written only in the base class, and all derived classes automatically treat it as a virtual function.
- The functions in the base and derived classes must have the same structure.
- If the function is not virtual, then the compiler decides which function to run based on the pointer type.
- If the function is virtual, the program uses late binding, meaning the compiler runs the function based on the actual object the pointer points to.
Syntax:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { // virtual keyword
cout << "Generic animal sound\n";
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Woof!\n";
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "Meow!\n";
}
};
int main() {
Animal* a; // base class pointer
a = new Dog();
a->sound(); // Output: Woof!
a = new Cat();
a->sound(); // Output: Meow!
return 0;
}Why use virtual functions?
- A virtual function ensures that the program calls the right function.
- It allows one function name to behave differently for different objects.
- You can add a new derived class without changing old code.
- When deleting an object through a base pointer, the virtual destructors ensure both base and derived destructors run properly.
Polymorphism with Virtual Functions
Polymorphism means one name and multiple behaviours. To get the runtime polymorphism, the base class function must be marked as virtual; it means that the program will decide at the runtime which function has to be called. There are two different type of polymorphism.

Disclaimer: We have provide you with the accurate handout of “Virtual Functions and Polymorphism in C++“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.
Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.