Polymorphism in C++ | Types of polymorphism | Compile time and Run time polymorphism | Example | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Monday, July 27, 2020

Polymorphism in C++ | Types of polymorphism | Compile time and Run time polymorphism | Example | My CS Tutorial


C++ Polymorphism | Types of polymorphism | Compile time polymorphism and examples | Runtime polymorphism and examples | My CS Tutorial

Polymorphism in C++ | My CS Tutorial

Polymorphism

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Polymorphism is one of the most principle of object oriented programming. Polymorphism means one name to different form. Polymorphism is the ability for a data to be processed in more than one form. Polymorphism that the same function name may behave differently and different class. The process of making an operator to exhibit different behaviour and different instant is known as operator overloading.
Polymorphism is a feature of OOPs that allows the object to behave differently in different conditions.
In C++ we have two types of polymorphism:

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

Function overloading and Operator overloading are perfect example of Compile time polymorphism.

Compile time Polymorphism Example

In this example, we have two functions with same name but different number of arguments. Based on how many parameters we pass during function call determines which function is to be called, this is why it is considered as an example of polymorphism because in different conditions the output is different. Since, the call is determined during compile time thats why it is called compile time polymorphism.

#include <iostream>
#include<conio.h>
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(20, 40)<<endl;
  //This will call the second function
  cout<<"Output: "<<obj.sum(45, 20, 62);
  return 0;
}

Output:

Output: 60
Output: 127

【2】 Runtime Polymorphism

Function overriding is an example of Runtime polymorphism.
Function Overriding: When child class declares a method, which is already present in the parent class then this is called function overriding, here child class overrides the parent class.

In case of function overriding we have two definitions of the same function, one is parent class and one in child class. The call to the function is determined at runtime to decide which definition of the function is to be called, thats the reason it is called runtime polymorphism.

Example of Runtime Polymorphism

#include <iostream>
using namespace std;
class X
 {
public:
  void disp()
  {
     cout<<"Super Class Function"<<endl;
  }
};
class Y: public X
{
public:
  void disp()
  {
     cout<<"Sub Class Function";
  }
};
int main()
{
  //Parent class object
  X obj;
  obj.disp();
  //Child class object
  Y obj2;
  obj2.disp();
  return 0;
}

Output:

Super Class Function
Sub Class Function


C++ Polymorphism | Types of polymorphism | Compile time polymorphism and examples | Runtime polymorphism and examples | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment