C++ Polymorphism in OOP | Types of Polymorphism in C++ with Examples | Method Overloading vs Overriding in OOP

C++ Polymorphism in OOP | Types of Polymorphism in C++ with Examples | Advantages of Polymorphism | Method Overloading vs Overriding | OOP Tutorial in C++

What is Polymorphism in Object Oriented Programming(OOP) ?

Polymorphism is considered as one of the important features of Object Oriented Programming which means having multiple forms of one thing that allows the object to behave differently in different conditions. 

It is a greek words that are “Poly” and “morphs” and basically translates to ‘many forms’.

Advantages of Polymorphism:

1. It helps the programmer to reuse the codes, i.e., classes once written, tested and implemented can be reused as required. Saves a lot of time.

2. Single variable can be used to store multiple data types.

3. Easy to debug the codes.

Real Life Example Of Polymorphism:

A real-life example of polymorphism, a person at the same time can have different characteristics.  Like a lady at the same time is a mother, a wife, an employee. So the same person posses different behavior in different situations. This is called polymorphism. 

Types of the polymorphism in C++:

There are generally two types of the polymorphism in C++:

1) Compile time Polymorphism – This is also known as static (or early) binding.
2) Runtime Polymorphism – This is also known as dynamic (or late) binding.


1) Compile time Polymorphism:

Whenever an object is bound with their functionality at the compile-time, this is known as the compile-time polymorphism. 

This type of polymorphism is achieved by function overloading or operator overloading which is also known as static binding or early binding.

C++ Function Overloading in OOP:

Function overloading means two or more functions share the same name but their parameters are different. Functions can be overloaded by change in number of arguments or/and change in type of arguments.

Example of Function Overloading in C++:

#include <iostream>

using namespace std;

 

class Add {

public:

  int sum(int num1, int num2){

     return num1+num2;

  }

  int sum(int num1, int num2, int num3){

     return num1+num2+num3;

  }};int main() {

  Add obj;

  //This will call the first function

  cout<<"Output: "<<obj.sum(10, 20)<<endl;

  //This will call the second function

  cout<<"Output: "<<obj.sum(11, 22, 33);

  return 0;

}

Output:

Output: 30

Output: 66

 

C++ Operator Overloading in OOP : 

Overloaded operator is used to perform operation on user-defined data type.

For example, we can make the operator (‘+’) for string class to concatenate two strings. 


Example of Operator Overloading in C++:

#include<iostream>

using namespace std;

 

class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i =0) {real = r; imag = i;}


// This is automatically called when '+' is used with

// between two Complex objects

Complex operator + (Complex const &obj) {

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

}

void print() { cout << real << " + i" << imag << endl; }

};

 

int main()

{

Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2; // An example call to "operator+"

c3.print();

}

Output:

12 + i9

2) Runtime polymorphism in OOP: 

Run time polymorphism is achieved when the object's method is invoked at the run time instead of compile time. This type of polymorphism is achieved by Function Overriding which is also known as dynamic binding or late binding.

What is Late binding or Dynamic linkage?

The process in which the function call is resolved during runtime is known as the late binding. Hence the type of object is determined by the compiler at the runtime and the function call is binded. Late Binding is also called Dynamic Binding or Runtime Binding.

C++ Function overriding in OOP: 

The function overriding means when the function has the same name, same return type and same parameters.

Requirements for Overriding a Function in OOP:

1. Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.

2. Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

Example of Function overriding in C++:

class Base{

    public:

    void show() {

        cout << "Base class\n";

}

};

class Derived:public Base{

public:

void show() {

        cout << "Derived Class\n";

}

};

int main() {

    Base b;       //Base class object

    Derived d;     //Derived class object

    b.show();     //Early Binding Ocuurs

d.show();   

}

Output:

Base class

Derived class


Difference between method overloading and method overriding in OOP:

There are many differences between method overloading and method overriding in OOP. A list of differences between method overloading and method overriding are given below:

No.

Method Overloading

Method Overriding

1)

Method overloading is used to increase the readability of the program.

Method overriding is used to provide the specific implementation of the method that is already provided by its super class.

2)

Method overloading is performed within class.

Method overriding occurs in two classes that have IS-A (inheritance) relationship.

3)

In case of method overloading, parameter must be different.

In case of method overriding, parameter must be same.

4)

Method overloading is the example of compile time polymorphism.

Method overriding is the example of run time polymorphism.

5)

method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Return type must be same or covariant in method overriding.

6)

A constructor can be overloaded

A constructor can not be overloaded

7)

It can occur without inheritance concept

It can not occur without inheritance concept

8)

A function can be overloaded any number of times

A function can be overriden only single number of times


Previous Post: 

C++ Encapsulation in OOP | Encapsulation in C++ with Examples | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation

0 Response to C++ Polymorphism in OOP | Types of Polymorphism in C++ with Examples | Method Overloading vs Overriding in OOP

Post a Comment