PLOYMORPHISM


                     PREVIOUS                                                                                                          NEXT

Polymorphism means an ability to assume different forms at different places. In OOP, it is a language's ability to handle objects differently based on their runtime type and use. Polymorphism is briefly described as "one interface, many implementations”. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

Example :- Since they are all of Felidae biological family, and they all should be able to meow, they can be represented as classes inheriting from Felid base class and overriding the meow pure virtual function,



poly

figure:-Polymorphic cats on a mat

Example :-We define a function called ‘concat’ which can take two parameters. Now if the parameters passed are of the type integer the function will return sum of two numbers whereas if the parameters passed are characters then the function will return two charters concatenated.In other words,
The + (plus) operator in C++:

    4 + 5      		 <-- integer addition 
    3.14 + 2.0  	 <-- floating point addition
    s1 + "bar"  	 <-- string concatenation! 

In C++, that type of polymorphism is called overloading.

There are two types of polymorphism:-

  • Compile time polymorphism - It is achieved by overloading functions and operators.
  • Run time polymorphism - It is achieved by overriding virtual functions.








Powered by Blog - Widget
Face Upward - Widget