Summary: In this tutorial, we will learn what polymorphism is, what are the different types of polymorphism and how it helps in reducing the complexity of the program in C++.

What is Polymorphism in C++?

Polymorphism according to the English dictionary means “the condition of occurring in several different forms”.

A real-life example of polymorphism is that a single person at the same time can be a father, a son, or an employee.

Similarly, in context to object-oriented programming, Polymorphism allows an interface (such as a function) to have different implementations (to perform different tasks depending on the conditions).

We can write multiple implementations for the same function and let the language decide which one to execute when.

How to Implement Polymorphism in C++?

In C++, we can implement polymorphism in the following ways:

  1. Function Overloading
  2. Function Overriding
  3. Operator Overloading

Depending on the implementation, Polymorphism can be categorized into two categories:

  1. Static Polymorphism
  2. Dynamic Polymorphism

Static Polymorphism

The polymorphism that occurs during the compile time is known as static polymorphism.

It happens when we overload any function or operator in C++.

Here is an example in C++ where we are overloading a function to implement static polymorphism:

#include <iostream>
using namespace std;

int add(int a, int b){
    return a+b;
}

double add(double a, double b){
    return a+b;
}

int main() {
    cout << add(5, 8) << endl;        //output 13
    cout << add(4.2, 2.3) << endl;    //output 6.5
    return 0;
}

In this example, we have defined two add functions with different types of parameters.

The compiler when scans the call statements in the main method, links them to the appropriate add method based on the parameters passed.

This linking of the function call statement to the corresponding function happens during the compile-time, hence it is called static linkage or early binding.

After the compilation when the program runs, we get the output as expected.

Dynamic Polymorphism

Unlike static polymorphism, dynamic polymorphism occurs during the runtime of the program.

It happens when we override a base class’s function in the subclass with the use of virtual keyword.

Here is an example in C++, where we are overriding a function to implement dynamic polymorphism:

#include <iostream>
using namespace std;

class Animal{
public:
    virtual void canWalk(){
        cout << "The Animal can walk.";
    }
};

class Dog: public Animal{
public:
    virtual void canWalk(){
        cout << "The Dog can walk.";
    }
};

int main() {
    Dog dog;
    dog.canWalk();    //output The Dog can walk.
    return 0;
}

In this example, we are overriding the Animal class’s canWalk method in the Dog class.

In response to the dog.canWalk() call statement, the Dog class’s canWalk method gets invoked.

The decision to call the method of the Dog class instead of the Animal class is made during the runtime of the program. It is therefore called dynamic or late binding.

Conclusion

Polymorphism is one of the main properties of any object-oriented programming language.

It allows an entity such as a function to have different implementations for different conditions.

Leave a Reply

14 − seven =