Polymorphism in C++

22 Feb 2023
Intermediate
153 Views

Polymorphism in C++

Introduction

Have you ever had the feeling that something was just a touch too complicated and your mind kept wrapping itself around ideas without actually putting them into practice? Well, if you're familiar with writing code in C++, then this is probably a very familiar feeling for you. And don't worry, because today we're going to learn about one of the core principles of programming in C++ programming language, which is polymorphism. So strap on your thinking caps and join us on this journey as we explore all of the amazing things that polymorphism has to offer and just how it makes life easier for C++ developers!

What is Polymorphism in C++

Polymorphism in C++ is a powerful programming technique that allows multiple characteristics of an object to be represented using the same interface. This polymorphic ability allows C++ developers to create code that is more flexible, modular, and easier to maintain. Polymorphism essentially allows different objects with different behaviors to interact as if they were all one type of object. This makes it easier for developers to take advantage of inheritance and code reuse by creating classes that don't need to be changed when a new behavior is needed within an application. Translating polymorphic principles into C++ code can be extremely beneficial for creating dynamic applications.

Real Life example of Polymorphism

Let's understand a real life example of Polymorphism. A lady who behaves like a teacher in a classroom, a mother with her children, or a wife in a home. So a single person has various roles in her life according to the situation.

Types of Polymorphism in C++

There are two types of Polymorphism in c++ programming language. Those are

  1. Compile time Polymorphism
  2. Runtime Polymorphism

Compile time Polymorphism

In compile time polymorphism the overloaded functions are invoked by matching the number and type of the arguments. This particular information is available at the time of compilation. It is achievable by doing function overloading and operator overloading that is known as early binding or static binding.

Example

class A // base class declaration. 
{ 
     int a; 
     public: 
     void display() 
       { 
             cout<< "Class A "; 
       } 
  }; 
class B : public A // derived class declaration. 
{ 
    int b; 
    public: 
   void display() 
  { 
        cout<<"Class B"; 
  } 
}; 

Runtime Polymorphism

Runtime Polymorphism in c++ programming language can be achieved when the method of the object is invoked at the runtime instead of compile time. This process can be achieved by method overriding.
Example
#include <iostream>
using namespace std;
class Animal
{
    public:
void eat()
    {
       cout<<"Eating...";
    }
};
class Dog: public Animal
{
 public:
 void eat()
    {
        cout<<"Eating fruit...";
    }
};
int main(void)
{
   Dog d = Dog();
   d.eat();
   return 0;
}

Difference between runtime and compile time polymorphism

Compile time polymorphismRun time polymorphism
In the compile time polymorphism, the function which is going to be invoked is known by the developersIn the run time polymorphism, the function which is going to be invoked is known at the run time
It is also known as static binding, overloading, and early binding.It is also known as late binding overriding, and Dynamic binding.
Overloading is a compile-time polymorphism where more than one method has the same name but with a different number of parameters or different types of parameters.Overriding is a run-time polymorphism where more than one method has the same name, number of parameters, and same type of parameters.
This particular polymorphism is achieved by operator overloading and function overloading.This particular polymorphism is achieved by pointers and virtual functions.
Compile time polymorphism in c++ programming language provides fast execution.Run time polymorphism in c++ programming language provides slow execution.
It is less flexible because mainly all the execution is done at the compile time.It is more flexible because all the execution is done at the run time

Summary

Polymorphism is a powerful tool that can be used in many different ways. When used correctly, it can make your code more concise and easier to read. It can also make your code more flexible and extensible. This article gave you a vast idea about various types of polymorphism in the c++ programming language including the difference between them.  As you learn more about C++, experiment with polymorphism and see how it can help you write better code.

Accept cookies & close this