Polymorphism in C++
Polymorphism:
Polymorphism means that ‘one name, multiple forms‘. It can be broadly classified into two categories:
- Compile-time Polymorphism
- Run-time Polymorphism
Compile-time Polymorphism:
Compile-time polymorphism means that an object is bound to its function call at the compile time. So, it is also called early binding or static binding or static linking.
Example:
#include<iostream.h> #include<conio.h> using namespace std; class A { public: void show() { cout<<"Bike is moving"; } }; class B: public A { public: void show() { cout<<"Car is moving"; } }; void main() { B b1; b1.show(); // calls derived class b1.A::show(); // calls base class getch(); }
Output:
Car is moving
Bike is moving
Run-time Polymorphism:
Run-time Polymorphism means that an object is bound to its function call at the run time. It defers the linking of a function call to a particular class at run-time. So it is called late binding or Dynamic binding or Dynamic linking.