Polymorphism

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.

Real Life Example Of Polymorphism

Let's take a real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. 


Let's take a real-world example of the word right can mean different things in a different context. 

  • I was right. In the above sentence, the word right means "correct". 
  • Please take a right turn. The word right refers to the "right direction" in the above sentence.

This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.


Example :
The + operator in C++ is one of the best examples of polymorphism. The + operator is used to add two integers as follows :  
    
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    int b = 20;
   cout << "Value of a + b is: " << a + b;
                return 0;
}


Output:
Value of a + b is : 30


However, + is also used to concatenate two string operands. The + operator is used to concatenate two strings as follows 


#include <iostream>
using namespace std;
 int main() {
        String a = "VVIT";
        String b = "Maranga";
   cout << "Value of a + b is: " << a + b;
      return 0;
}


Output:
Value of a + b is : VVITMaranga


 

 

    

 

 

No comments:

Post a Comment