If you are already known to programming and ever heard about Object Oriented Programming (OOP) then you must have heard the word polymorphism which is one of the major pillar of OOP.
In simplest term polymorphism in C++ is using one thing in many forms. The most common example is a human that perform different tasks at different places such as in office he/she is an employee, whereas in market he/she is a customer and in college or school a student.
You will also read: Why Learn C++
1. Example : we use one word at different situation with different meanings in English language.
2. Example : many of us are fan of cartoon series such as ben ten who takes different characters according to the situation, which fits as an example of polymorphism in C++.
three types of polymorphism in C++ are used in programming languages:
follows the early binding concept in which the attributes are bind to its entities at compile time. The scope is same in this type.
Overloading is achieved in this type of polymorphism, such as
function with same name, but defined multiple time within same scope (class) and to make it easy for compiler to bind the function call with right function definition some differences are there such as number of arguments passed and types of arguments used in function.
Example: using C++
in OOP constructor is similar to function so passing arguments to the function is also possible.
One can overload it using parameterised constructor concept.
Example: using C++
when one wants to use an operator to perform operation on user defined type of value such as addition between objects of the class then this term is used.
it is an advantage over function overloading concept as in overloading same function is defined multiple time which means code is not reduced but only redefined with different number or types of parameter but in case a programmer wants to use only one function for all kind of operations he/she can find it useful for such operation.
Templates or generics are example of this type.
Templates are able to hold any type of value similar like one can get multiple things from a general store that is why these are also called generics.
considered as run time polymorphism as the binding task is performed at runtime (late binding).
Overriding is used to achieve this polymorphism.
in it the scope is different i.e., same function or method is overridden (defined) in different classes without changing its number and types of parameter.
Error: cannot create the instance of abstract base class.