Home > OOP > Polymorphism in C++

Polymorphism is the combination of two words poly (many) and morphs (shapes), thus meaning many shapes. It is a phenomenon representing one form in multiple forms.Typically, it occurs when classes are related by inheritance. The philosophy that lies behind polymorphism is we have one interface with its many implementations. It allows the single interface to gain access to the generic class of actions. In real Life example, we can say that a human can act in different ways. Like while traveling it acts like a passenger. In the institute, he/she acts as a student. At home, he/she acts as father/mother or son/daughter or brother/sister. Thus it can have many shapes depending on its needs or surroundings. Similarly, Polymorphism in c++ means that call to a member function will invoke a different function for execution depending on the type of object which is calling a member function. There are two types of polymorphism in c++

Types of Polymorphism in C++

  1. Compile time (Early Binding).
  2. Runtime(Late Binding).

Compile Time Polymorphism in C++ (Early Binding)

Sometimes you may refer it as early binding. It is a type in which we decide the function call at the time of compiling a program. Static binding fixes all types of variables and expressions and function calls at the time of compiling the program.You can achieve it by using the following two concepts of Object-Oriented programming in C++:

  1. Function Overloading
  2. Function Overriding

Example: –

#include "stdafx.h"
#include "iostream"
using namespace std;

class Weapon
{
     public:
          void loadFeatures()
          {
               cout << "Loading weapon features.\n";
          }
};

class Bomb : public Weapon
{
     public:
          void loadFeatures()
          {
               cout << "Loading bomb features.\n";
          }
};

class Gun : public Weapon
{
     public:
          void loadFeatures()
          {
               cout << "Loading gun features.\n";          
          }
};

int main()
{
     Weapon *w = new Weapon;
     Bomb *b = new Bomb;
     Gun *g = new Gun;

     w->loadFeatures();
     b->loadFeatures();
     g->loadFeatures();
     return 0;
}

Output: –
Types of Polymorphism in c++ compile time- early Binding
As from its output, it is clear that call to a member function is decided at compile time. Since each object calls its own member function which was decided at compile time.

Runtime polymorphism in C++ (Late Binding)

Sometimes you may refer it as Late Binding. As the name suggests it will decide the function call at runtime. In late binding, we don’t fix the function call. That’s why you may refer it as dynamic binding. You can achieve it by the use of virtual functions.

Example: –

#include "stdafx.h"
#include "iostream"
using namespace std;
class Weapon
{
     public:
          virtual void features()
          {
               cout << "Loading weapon features.\n";
          }
};
class Bomb : public Weapon
{
     public:
     void features()
     {
          cout << "Loading bomb features.\n";
     }
};
class Gun : public Weapon
{
     public:
     void features()
     {
          cout << "Loading gun features.\n";
     }
};
class Loader
{
     public:
     void loadFeatures(Weapon *weapon)
     {
          weapon->features();
     }
};
int main()
{
     Loader *l = new Loader;
     Weapon *w, w1;
     Bomb b;
     Gun g;
     w = &w1;
     l->loadFeatures(w);
     w = &b;
     l->loadFeatures(w);
     w = &g;
     l->loadFeatures(w);
     return 0;
}

Output: –
Types of Polymorphism in c++ Runtime-Late Binding
In this code, we can see that we are calling the member function of loader class which decides at runtime to call a member function depending on the object passed as a parameter to it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.