Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms. It is a greek word. In object-oriented programming, we use 3 main concepts:
inheritance, encapsulation, and polymorphism.
Real Life Example Of Polymorphism
Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a
classroom, mother or daughter in a home and customer in a market. Here, a single
person is behaving differently according to the situations.
o Compile time polymorphism: The overloaded functions are invoked by
matching the type and number of arguments. This information is available at the
compile time and, therefore, compiler selects the appropriate function at the
compile time. It is achieved by function overloading and operator overloading
which is also known as static binding or early binding. Now, let's consider the
case where function name and prototype is same.
o Run time polymorphism: Run time polymorphism is achieved when the object's
method is invoked at the run time instead of compile time. It is achieved by
method overriding which is also known as dynamic binding or late binding.
,Differences b/w compile time and run time polymorphism.
Compile time polymorphism Run time polymorphism
The function to be invoked is known at the compile time. The function to be invoked is known at the
time.
It is also known as overloading, early binding and static It is also known as overriding, Dynamic bind
binding. and late binding.
Overloading is a compile time polymorphism where Overriding is a run time polymorphism wh
more than one method is having the same name but more than one method is having the same na
with the different number of parameters or the type of number of parameters and the type of
the parameters. parameters.
It is achieved by function overloading and operator It is achieved by virtual functions and pointers.
overloading.
It provides fast execution as it is known at the compile It provides slow execution as it is known at the
time. time.
It is less flexible as mainly all the things execute at the It is more flexible as all the things execute at
compile time. run time.
CONCEPT OF Function Overloading
Function Overloading is defined as the process of having two or more function with the
same name, but different in parameters is known as function overloading in C++. In
function overloading, the function is redefined by using either different types of
arguments or a different number of arguments. It is only through these differences
compiler can differentiate between the functions.
, The advantage of Function overloading is that it increases the readability of the
program because you don't need to use different names for the same action.
C++ Function Overloading Example
Let's see the simple example of function overloading where we are changing number of
arguments of add() method.
// program of function overloading when number of arguments vary.
1. #include <iostream>
2. using namespace std;
3. class Cal {
4. public:
5. static int add(int a,int b){
6. return a + b;
7. }
8. static int add(int a, int b, int c)
9. {
10. return a + b + c;
11. }
12. };
13. int main(void) {
14. Cal C; // class object declaration.
15. cout<<C.add(10, 20)<<endl;
16. cout<<C.add(12, 20, 23);
17. return 0;
18. }
Output:
30
55
C++ Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data
type, this ability is known as operator overloading. Operator overloading is a
compile-time polymorphism. For example, we can overload an operator ‘+’ in a
class like String so that we can concatenate two strings by just using +. Other