Polymorphism in C++

Polymorphism means “many forms.” In simple terms, it refers to the ability of something to take on different forms or behaviors in different situations.

A real-life example of polymorphism is a person who can play different roles at the same time. For instance, a man can be a father, a husband, and an employee all at once. This is an example of polymorphism.

Polymorphism is an important concept in Object-Oriented Programming (OOP). In C++, there are two main types of polymorphism:

Polymorphism in C++

1. Compile-time Polymorphism (Static Polymorphism / Early Binding):

  • This happens when functions or operators are overloaded.
  • Function overloading means defining multiple functions with the same name but different parameters (either a different number of arguments or different types of arguments).
  • Operator overloading means giving a new meaning to an operator (like +, -, etc.) for user-defined types (classes).

Compile-time polymorphism is determined at compile time, which is why it’s called static polymorphism or early binding.

Example (Function Overloading) in C++

#include <iostream>
using namespace std;

class Printer {
public:
void print(int i) {
cout << “Printing integer: ” << i << endl;
}

void print(double d) {
cout << “Printing double: ” << d << endl;
}
};

int main() {
Printer p;
p.print(5); // Calls print(int)
p.print(3.14); // Calls print(double)
return 0;
}

Output:

Printing integer: 5
Printing double: 3.14

2. Runtime Polymorphism (Dynamic Polymorphism / Late Binding)

  • Runtime Polymorphism is enabled by virtual functions and function-overriding
  • Function overriding occurs when a function is overridden in a derived class. In this case, the function in the base class is redefined in the derived class with the same signature.
  • The decision about which function to call is made at runtime, which is why it’s called dynamic polymorphism or late binding.
Important: Virtual functions and function overriding are related, they are not the same concept.

Example (Function Overriding) in C++

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
// Function in base class
void speak() {
cout << “Animal speaks” << endl;
}
};

// Derived class
class Dog : public Animal {
public:
// Overriding the speak function in derived class
void speak() {
cout << “Dog barks” << endl;
}
};

int main() {
// Create objects of base and derived classes
Animal animal;
Dog dog;

// Call speak() method on both objects
animal.speak(); // Calls Animal’s speak() method
dog.speak(); // Calls Dog’s overridden speak() method

return 0;
}

Output

Animal speaks
Dog barks

Explanation

  • Base class Animal: It has a speak() function, which prints “Animal speaks.”
  • Derived class Dog: It overrides the speak() function to print “Dog barks” instead.
  • Function Overriding: In C++, when a function in the derived class has the same name and signature as a function in the base class, it overrides the base class function.