We already know what polymorphism is in this unit we shall try to understand the two ways in which polymorphism is implemented in C++.
When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by a change in a number of arguments or/and a change in the type of arguments.
Functions signature refers to the
- return-type of the function.
- data-type of the passed variables
- and finally the number of arguments being passed to the function
Consider an example program to understand function overloading in C++.
In the above example, a single function named function acts differently in three different situations which is the property of polymorphism.
C++ also offers option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands, concatenates them.
In the above example the operator ‘+’ is overloaded. The operator ‘+’ is an addition operator and can add two numbers(integers or floating-point) but here the operator is made to perform addition of two imaginary or complex numbers.