Polymorphism – C++

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.

Before getting into this section, it is recommended that you have a proper understanding of pointers and class inheritance.

There are two types of polymorphism:

  • Static Polymorphism
  • Dynamic Polymorphism
Static Polymorphism: Any entity in which can exist in different physical forms at the same time. As the different types of parameters are specified in declaration of function so calls can be made at compile time. Number, type, and sequence of parameters declared for each form of the function will decide which function will get called. This is also called as Early Binding.
For example: add(int, int);  and add(float , float);
In the above example if we pass 2 integers then the first function will get called and if we pass 2 float values then the second function will get called.
Dynamic Polymorphism: When any entity change its form depending upon circumstances then that kind of polymorphism is called as Dynamic polymorphism.A function is said to exhibit dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program is executed. This is also called as Late Binding.
Advertisements