Polymorphism in C++
Polymorphism is the technique of using same thing for different purpose.
There are two types of polymorphism:
(A) Compile time polymorphism
(B) Run time polymorphism
(A) Compile time polymorphism:
Compile time polymorphism is also known as static binding or early binding.
Function overloading and Operator overloading are the example of compile time polymorphism.
It is called compile time polymorphism because which version of function to invoke is determined by the compiler at compile time based on number and types of the argument.
Thus in compile time polymorphism which function to invoke is determined at compile time so it is called static or early binding.
(B) Run time polymorphism:
Run time polymorphism is also known as dynamic binding or late binding.
Virtual function is the example of run time polymorphism.
While inheriting derived class from base class if both classes contain same function then we have to declare that function as a virtual in the base class.
In order to invoke function from the appropriate class you need to declare a pointer of base class and then invoke the function using that pointer. If pointer contains the address of the base class object then base class version is invoked and if pointer contains the address of derive class object then derived class version is invoked.
Thus in run time polymorphism which function to invoke is determined at runtime so it is called dynamic binding or late binding.