C++ Polymorphism

Polymorphism in c++ is the capability of objects of different types to respond to functions of the same name. The program does not know the actual object type in advance.

A word polymorphism is a mixture of two works ploy and morphism where poly means many and morphism means forms. It is applicable when the classes have interacted with each other through inheritance.

To understand the polymorphism concept we have to take three sentences which are given below.  

1) You right to know this

2) Please turn right

3) We are in a right place

In all the above conditions, the single word “right” behaves differently in different conditions.  If we treat right as a single word we don’t know the actual meaning to consider.


Polymorphism in c++ can be acchived by the following 4 ways

  1. Function overloading
  2. Operator overloading
  3. Function Overriding
  4. Using Virtual Function

Here we discuss one by one


1. Function overloading:

It can be achieved by using the same name functions but with different parameters. Below is the example

Example 1:

//Function overloading in C++
#include <iostream>
using namespace std;
class Addition {
public:
  int sum(int number1, int number2){
     return number1+number2;
  }
  int sum(int number1, int number2, int number3){
     return number1+number2+number3;
  }
};
int main() {
  Addition add;;
  
  cout<<"Output: "<<add.sum(55, 45)<<endl;
  cout<<"Output: "<<add.sum(55, 45, 100);
  return 0;
}

Output

Output: 100
Output: 200

2. Operator overloading

It can be achieved by using operator overloading. For example we use “+” operator for addition of two operands but at the same time we use it to concatenate two strings. Below is the example

Exemple 2:

// Operator overloading in C++

#include <iostream>
using namespace std;

class Sum {
	int number1;
	public: 
	Sum(int n)
	{
		number1 = n;
	}
	Sum operator+(Sum s2)
	{
		Sum s3(0);
		s3.number1 = number1+s2.number1;
		return s3;
	}
	void Show()
	{
		cout<<"Result will be: "<<number1<<endl;
	}
  
};

int main() {
	Sum s1(5);
	Sum s2(25);
	Sum result(0);
	result=s1+s2;
    result.Show();
    return 0;
}

Output

Result will be: 30

3. Function Overriding

It can be achieve by declaring two methods having same definition in parent and the child class. This is called function overriding. It can be determined at run time that which definition of the function is going to call.

Example 3:

// Function Overriding in C++

#include <iostream>
using namespace std;

class Parent {
   public:
    int Sum(int Number1, int Number2) {
    	int result = Number1+Number2;
        cout << "Sum at parent class: " <<result<< endl;
    }
};

class Child : public Parent {
   public:
      int Sum(int Number1, int Number2) {
    	int result = Number1+Number2;
        cout << "Sum at child class: " <<result<< endl;
    }
};
int main() {
    Parent parent1;
    parent1.Sum(5,3);
    return 0;
}

Output

Sum at parent class: 8

4. Using Virtual Function

As is name concerns these functions are effective but in reality does not exist known as virtual functions. These functions enable users to execute completely different functions with same name from different classes by the same function call. These functions defined with the keyword “virtual” in the parent class and can be overridden in the child class.

Example 4

// Polymorphism in C++

#include <iostream>
using namespace std;

class Parent {
   public:
   virtual int Sum(int Number1, int Number2) {
    	int result = Number1+Number2;
        cout << "Sum at parent class: " <<result<< endl;
    }
};

class Child : public Parent {
   public:
      int Sum(int Number1, int Number2) {
    	int result = Number1+Number2;
        cout << "Sum at child class: " <<result<< endl;
    }
};
int main() {
	
	Child child1;
	Parent* parent1 = &child1;
    parent1->Sum(5,15);
    return 0;
}

Output

Sum at child class: 20

Leave a Reply