One of the important features of any programming language like C++ is "Polymorphism". Poly means "many" and "morphos" means "forms". Therefore, Polymorhism is essentially the ability of an object to take many or multiple forms as per the context.
Polymorphism is not a concept of OOPS or programming only, in real life there are many examples of the same theory. Take for example the english word "bank", in the language it can stand for a money bank or bank of a river. What exactly the word means can only be understood by looking at the context. For example if there is talk going on about money or other monetary issue the word can be for the financial bank.
In C++, the word mainly stands for collectively referring to two activities or concepts - Function or method overloading and Operator Overloading.
Function Overloading : In C, it is not possible to declare two functions with the same name. For example, if a program has a function named "show()", the same name cannot be used for declaring another function. In C++, it is possible. The process of declaring two or more methods with the same name is called "Function Overloading". But there is a catch, all the functions with the same name must be different in their "signature". The term signature implies that the functions must be different in their arguments. All the functions that are intended to be overloaded must be differ in any of the following
a) Different number of arguments
b) Different datatypes of arguments
c) Order of arguments
Operator Overloading : There are two types of operators
a) Unary and
b) Binary
The unary operator are those operators that take a single operand as argument. For example ++ and --, whereas Binary operators are those operators that take two operands, for example +, - , * , / etc.
Every operator has a specific task. For example '+' adds two numericals, '-' subtracts the values etc. Operator overloading the ability of an operator to change the basic function of an operator to perform any other function other than its core task.
0 comments:
Post a Comment