C++ Polymorphism

The word polymorphism means having many forms. In other words, Polymorphism means more than one function with the same name, with different working. Polymorphism can be both static and dynamic. In static polymorphism, the memory will be allocated at compile-time. When it is dynamic polymorphism memory will be allocated at run-time.

In C++ we have two types of polymorphism:

  • Compile time Polymorphism
  • Runtime Polymorphism

Compile time Polymorphism

Compile time binding is to associate a function’s name with the entry point of the function at compile time.Function overloading and operator overloading are used to achieve compile time polymorphism.

Runtime Polymorphism

Runtime Polymorphism is a form of polymorphism at which function binding occurs at runtime. You can have a parameter in the subclass, the same as the parameters in its super classes with the same name. Virtual keyword is used in superclass to call the subclass. The function call takes place on the run time, not on the compile time. A virtual function is used to achieve runtime polymorphism.