Hello friends, in this post today we learn about what is Polymorphism in c++. so lets start...
![]() |
Polymorphism |
Polymorphism:-
The Polymorphism word comes from 2 greek words: poly and morphsim.
The word "poly" means many and "morphsim" means forms.
So polymorphism means many forms.
Polymorphism means the ability to take more than one form.
It allows a single name to be used for more than one related purpose.
It means ability of operators and functions to act differently in different situations.
Before the learn types of polymorphism first of all we understand what is binding.
Binding is the linking of function call with the place where function definition is written.
When binding is done at compile time it is known as early-binding, compile time binding or static binding.
When binding is done at run time it is known as late-binding, runtime binding or dynamic binding.
Types of polymorphism:-
1)-Compile time polymorphism:
Compile time polymorphism is a such type of polymorphism in which the overloaded member functions are selecting for invoking by matching argument at the time of compilation of the program. The early binding or static binding concept is introduced. static binding means choosing a function call at the time of compilation.
a). Function Overloading:
Function overloading is the practice of declaring the same function with different signatures.
Function overloading refers to creating numbers of functions with the same name which performs different tasks.
Overloading of functions with different return type is not allowed.
b). Operator Overloading:
Operator overloading refers to overloading of one operator for many different purpose.
For example, the binary + can be used to add two integer numbers, two float numbers, two structures variables, two union variables or two class objects.
2)-Run time polymorphism :
Runtime polymorphism is a such type of polymorphism in which the selection of member function is done while the program is running. The late binding or dynamic binding concept is introduced. Late binding means choosing a function call while the program is running. In late binding the function link with class very late. so it is known as late binding. Late binding is implement using virtual function.
At run-time, the code matching the object under current reference will be called.
a). Virtual Function:
Virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.
The whole function body can be replaced with a new set of implementation in the derived class. It is declared as virtual in the base class using the virtual keyword.
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with something new information .
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment