Concept of Polymorphism in C++

Concept of Polymorphism in C++

Polymorphism is one of the most important features of Object Oriented Programming.  Polymorphic variables can take up objects or values of different types as values and polymorphic functions and procedures can be used as objects or values of different types.  It refers to exhibiting different behaviour by an instance in different situations.  Polymorphism is implemented through function overloading and operator overloading.  Overloading is a form of polymorphism.  In case of function overloading, the information regarding which function is to be invoked corresponding to a function call is available at compile time.  This is referred to as static binding or compile time polymorphism, as the object is bound with its function call at compile time.

Run time polymorphism is achieved through virtual functions.  The function to be invoked by the instance of a class is known during runtime.  The functions are linked with the class during runtime and not during compile time.  When a base class pointer points to a sub class instance, instead of invoking subclass function (a function having name similar to that of function of a base class), it always invokes base class version.  If the function in the base class is defined as virtual, then it is known during runtime only, which version of the function will be invoked, corresponding to a function call.  Since the function is linked after compilation process, it is termed as late binding or dynamic binding or run-time polymorphism