C++ | Polymorphism

The word polymorphism means having many forms. In simple words, we will define polymorphism because of the ability of a message to be displayed in additional than one form.

Real-life example - A person at the same time can have different characteristics. A man at an equivalent time may be a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.


In C++ polymorphism is mainly divided into two types:

1. Compile-time Polymorphism- (This is also known as static (or early) binding.)

1.Function Overloading

2.Operator Overloading

2. Runtime Polymorphism- (This is also known as dynamic (or late) binding.)

1.Virtual Function


Compile-time polymorphism:

This type of polymorphism is achieved by function overloading or operator overloading.

  • Function Overloading - When there are multiple functions with an equivalent name but different parameters then these functions are said to be overloaded. Functions are often overloaded by the change within the number of arguments or/and changes within the sort of arguments.

#include <bits/stdc++.h> 
using namespace std; 
class PragrammersDoor
{ 
	public: 
	void func(int x) 
	{ 
		cout << "value of x is " << x << endl; 
	} 
	void func(double x) 
	{ 
		cout << "value of x is " << x << endl; 
	} 
	void func(int x, int y) 
	{ 
		cout << "value of x and y is " << x << ", " << y << endl; 
	} 
}; 

int main() { 
	ProgrammersDoor obj1; 
	obj1.func(7); 
	obj1.func(9.132); 
	obj1.func(85,64); 
	return 0; 
} 

Output:
Value of x is 7
value of x is 9.132
value of x and y is 85, 64
  • Operator overloading- C++ also provides the choice to overload operators. for instance, we will make the operator (‘+’) for string class to concatenate two strings. we all know that this is often the addition operator whose task is to feature two operands. So one operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.

#include<iostream> 
using namespace std; 

class Complex { 
private: 
	int real, imag; 
public: 
	Complex(int r = 0, int i =0) {real = r; imag = i;} 
	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; 
	c3.print(); 
} 
Output:
12 + i9

In the above example, the operator ‘+’ is overloaded. The operator ‘+’ is an addition operator and can add two numbers(integers or floating-point) but here the operator is made to perform the addition of two imaginary or complex numbers.


Runtime Polymorphism:

Function overriding is an example of Runtime polymorphism.

  • Function Overriding: When child class declares away, which is already present within the parent class then this is often called function overriding, here child class overrides the parent class.

  • In the case of function overriding, we've two definitions of an equivalent function, one is parent class and one in child class. the decision to the function is decided at runtime to make a decision which definition of the function is to be called, that is the reason it's called runtime polymorphism.

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

Output:

Super Class Function
Sub Class Function

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

#blog #interview #placement #learn #computer #science

        Contact Us

programmersdoor@gmail.com

  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door