1.

Compare compile time polymorphism and Runtime polymorphism

Answer»

The main difference between COMPILE-time and runtime polymorphism is provided below:

Compile-time polymorphismRun time polymorphism
In this method, we would come to know at compile time which method will be called. And the call is RESOLVED by the compiler.In this method, we come to know at run time which method will be called. The call is not resolved by the compiler.
It provides fast execution because it is known at the compile time.It provides slow execution COMPARED to compile-time polymorphism because it is known at the run time.
It is achieved by function overloading and operator overloading.It can be achieved by virtual FUNCTIONS and pointers.

Example -

int add(int a, int B){ return a+b;}int add(int a, int b, int c){ return a+b+c;}int main(){ cout<<add(2,3)<<endl; cout<<add(2,3,4)<<endl; return 0;}

 

Example -

class A{ public: virtual void fun(){ cout<<"base "; }};class B: public A{ public: void fun(){ cout<<"derived "; }};int main(){ A *a=new B; a->fun(); return 0;}


Discussion

No Comment Found