Virtual Functions and Runtime Polymorphism
Introduction to Virtual Functions and Runtime Polymorphism
Virtual functions and runtime polymorphism are important concepts in object-oriented programming. They allow a program to work with objects of different classes in a uniform way by treating them as instances of a common base class. This is accomplished by using virtual functions, which allow functions to be bound to their implementations at a later time or during runtime.
Understanding Virtual Functions
Virtual functions are functions that are declared in a base class and overridden in derived classes. When a function is called using a pointer or reference to the base class, they permit derived classes to offer their own implementation of the function, which is invoked in place of the base class implementation.
For example, consider the following code:
#include<iostream>
using namespace std;
class Animal {
public:
virtual void makeSound() {
cout << "The animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "The dog barks" << endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
cout << "The cat meows" << endl;
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->makeSound();
animal2->makeSound();
delete animal1;
delete animal2;
return 0;
}
Explanation:
- First, a base class Animal is defined, which has a virtual function makeSound().
- The keyword virtual denotes that derived classes have the ability to override this function. The implementation of this function in the base class simply outputs a generic message saying, "The animal makes a sound".
- Two derived classes, Dog and Cat, are then defined, which inherit from the Animal class.
- They each provide their own implementation of the makeSound() function, which outputs a more specific message saying, "The dog barks" or "The cat meows".
- In the main() function, two pointers of type Animal* are created, which are initialized to point to new instances of the Dog and Cat classes, respectively. This is an example of polymorphism, as the derived classes are treated as if they were instances of the base class.
- Next, the makeSound() function is called on both pointers using the -> operator.
- The result demonstrates that the function is called according to the actual type of the object referenced to rather than the static type of the pointer.
- With the delete operator, the objects' dynamically allocated memory is finally released.
Program Output:

Polymorphism
In object-oriented programming, polymorphism is a key idea that enables programs to interact uniformly with objects from various classes. Compile-time polymorphism and run-time polymorphism are the two primary varieties of polymorphism.
Compile-time polymorphism is achieved through the use of function overloading or templates, which allow multiple functions with the same name to be defined, but with different argument types or numbers. Based on the static types of the arguments, the correct function is chosen at compile time.
Runtime polymorphism, on the other hand, is achieved through the use of virtual functions and inheritance, which allow derived classes to provide their own implementation of a function that is declared in the base class. In contrast to the compile-time selection of the proper implementation of the function based on the static type of the pointer or reference, the runtime selection is based on the actual type of the object.
Pure Virtual Functions and Abstract Classes
When a virtual function is declared in a base class but not implemented there, the function is said to be pure virtual. As an alternative, derived classes are supposed to replace them. An abstract class, which cannot be instantiated, is one that has one or more pure virtual functions. Instead, it is intended to be used as a base class for derived classes that offer their own implementation of the pure virtual functions.
For example, consider the following code:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void makeSound() = 0; // pure virtual function
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "Woof!" << endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
cout << "Meow!" << endl;
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->makeSound();
animal2->makeSound();
delete animal1;
delete animal2;
return 0;
}
Explanation:
- This program demonstrates the concept of abstract classes, virtual functions, and runtime polymorphism in C++.
- First, we define an abstract class Animal which contains a pure virtual function makeSound().
- The ‘= 0’ syntax for the pure virtual function indicates that it has no implementation in the base class and that derived classes must implement it. This makes the class Animal an abstract class which cannot be instantiated.
- Next, we define two derived classes, Dog and Cat, both of which inherit from the Animal class. In each derived class, we override the makeSound() function and provide a unique implementation for the sound that each animal makes.
- In the main() function, we create two pointers of type Animal*, animal1 and animal2, and assign them to newly created Dog and Cat objects using dynamic memory allocation.
- We then call the makeSound() function on each of these objects using the arrow operator ->.
- Since the makeSound() function is a virtual function, the appropriate implementation of this function for the respective object is called at runtime. In other words, we achieve runtime polymorphism.
- Finally, we delete the dynamically allocated objects to free up memory
Program Output:

Late Binding and Runtime Polymorphism
Virtual functions enable late binding or runtime binding of functions to their implementations. In other words, rather than being decided upon at compile time based on the static type of the pointer or reference, the implementation of the function is decided upon at runtime based on the actual type of the object.
For example, in the previous example, the makeSound() function is called on objects of type Animal*, which is the static type of the pointers. The function that is called, however, is actually implemented depending on the actual type of the objects, which are Dog and Cat, at runtime.
This behaviour is known as runtime polymorphism because the behaviour of the program is determined at runtime based on the actual types of the objects rather than at compile-time based on the static types of the pointers or references.
Advantages of Virtual Functions and Runtime Polymorphism
Virtual functions and runtime polymorphism provide several advantages in object-oriented programming. They enable a program to work with objects of different classes in a uniform way, by treating them as instances of a common base class. This can simplify the code and make it more flexible, as new derived classes can be added without modifying the existing code.
Virtual functions also enable dynamic dispatch, which means that the appropriate implementation of a function is selected at runtime based on the actual type of the object. This can improve the performance of the program, as it avoids the need for complex if-else or switch statements to select the appropriate implementation of the function.
Limitations of Virtual Functions and Runtime Polymorphism
Virtual functions and runtime polymorphism also have some limitations in object-oriented programming. They can introduce some overhead in the program, as the function call must be resolved at runtime, which can be slower than calling a non-virtual function that is resolved at compile-time.
Another limitation is that virtual functions can only be used with pointer or reference types, not with objects. As a result, objects of a derived class cannot be utilised as parameters or return values of functions that need objects of the base class,
In derived classes, virtual functions can be overridden, but they cannot be concealed. This means that if a derived class defines a function with the same name as a virtual function in the base class, the derived class function will always be called, even if the object is treated as an instance of the base class.
Conclusion
In object-oriented programming, runtime polymorphism and virtual functions are key ideas.
They enable a program to work with objects of different classes in a uniform way, by treating them as instances of a common base class. By using virtual functions, which enable runtime or late binding of functions to their implementations, this is accomplished.
Although virtual functions and runtime polymorphism have some limitations, they provide several advantages in object-oriented programming, including flexibility, code simplification, and the dynamic dispatch.