Polymorphism means more than one function with same name, with different working. Polymorphism can be static or dynamic.
In static polymorphism memory will be allocated at compile-time. In dynamic polymorphism memory will be allocated at run-time.
Both function overloading and operator overloading are an examples of static polymorphism.
Virtual function is an example of dynamic polymorphism.
- Static polymorphism is also known as early binding and compile-time polymorphism.
- Dynamic polymorphism is also known as late binding and run-time polymorphism.
- Function Overloading : Function overloading is an example of static polymorphism. More than one function with same name, with different signature in a class
or in a same scope is called function overloading. More info.
- Operator Overloading : Another example of static polymorphism is Operator overloading. Operator overloading is a way of providing new implementation of
existing operators to work with user-defined data types. More info.
- Virtual function : Virtual function is an example of dynamic polymorphism. Virtual function is used in situation, when we need to invoke derived class function using
base class pointer. Giving new implementation of derived class method into base class and the calling of this new implemented
function with base class's object is done by making base class function as virtual function. This is how we can achieve "Runtime Polymorphism".