In this article types of polymorphism we give the information about polymorphism means many forms. It is an ability by which a message is displayed in many forms.

Polymorphism in C++

  • In C++, polymorphism means many forms.
  • It is made up of two words poly and forms. It’s a geek word.
  • Simply put, “polymorphism means having many forms.”
  • Polymorphism is an ability by which a message is displayed in many forms.
  • In C++, Polymorphism occurs when we have multiple classes which are related to each other through inheritance.

Real life example of Polymorphism:-

A woman is a teacher in a college and that woman is a mother or daughter in her home and there is a customer in the market. Here there are different forms according to the situations of a woman. This is called polymorphism. This is a very important concept of object oriented programming (OOPs).

Types of Polymorphism in C++: 

There are two types of Polymorphism in C++ such as follows,

  1. Compile time polymorphism
  2. Run time polymorphism

Types of Polymorphism

1) Compile time polymorphism:-

This type of polymorphism is achieved by function overloading or operator overloading. It is also called static or early binding. Overloaded functions are invoked at compile time based on the type and number of arguments. In this the compiler selects the right function at compile time.

Function overloading –

When multiple functions have the same name but their parameters are different, it is called function overloading. Functions can be overloaded by changing the number of arguments or by changing the type of arguments.

Example –

#include<iostream.h>

#include<iomanip.h>

using namespace std;

class Add

{

public:

int sum(int n1, int n2)

{

return n1+n2;

}

int sum(int n1, int n2, int n3)

{

return n1+n2+n3;

}

};

int main()

{

Add p;

cout<<“Sum : “<<p.sum(22, 44)<<endl;

cout<<“Total: “<<p.sum(20, 50, 60);

return 0;

}

OUTPUT:-

Sum: 66
Total: 130

Operator Overloading –

We can also overload operators in C++.

It is a compile time polymorphism in which the operator is overloaded to give a special meaning to the user-defined data type.

For example – we can concatenate two strings by ‘+’ operator. We know that + is an addition operator which is used to add two operands. So + is used for addition to integer operands and also to concatenate strings.

We cannot use operator overloading with basic data types like: – int, double etc.

Example –

#include<iostream.h>

using namespace std;

class Count

{

private:

int value;

public:

// Constructor to initialize count to 5

Count() : value(7) {} // Overload ++ when used as prefix

void operator ++()

{

value = value + 1;

}

void display()

{

cout << “Count: ” << value << endl;

}

};

int main()

{

Count count1; // Call the “void operator ++()” function++

count1;

count1.display();

return 0;

}

OUTPUT:-
Count: 8

2) Runtime polymorphism:-

This type of Polymorphism is achieved by Function Overriding. In this the object method is invoked at run time instead of compile time. It is also called dynamic or late binding.

Function Overriding –

Function overriding occurs when the derived class has definitions for the member functions of the base class. That is, both the base and derived classes have the same name function defined.

 #include<iostream.h>

using namespace std;

class A

{

public:

void display()

{

cout<<“Base class”;

}

};

class B:public A

{

public: void display()

{

cout<<“Derived Class”;

}

};

int main()

{

B obj;

obj.display();

return 0;

}

OUTPUT:-
Derived Class

 

Difference between Compile time and Run time polymorphism:-

Compile-time:-

In this the function is invoked at compile time.

It is also called early binding or static binding.

This is achieved by function overloading and operator overloading.

It is less flexible.

It provides fast execution.

Run time:-

In this the function is invoked at run time.

It is also called dynamic binding or late binding.

This is achieved through virtual functions and pointers.

It is more flexible.

It provides slow execution.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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