Friday, 10 March 2017

Polymorphism

Polymorphism Banner


“One name many form.”

Polymorphism gives the ability to dynamic programming to a language which reduces to write repeated code for each module. You can use polymorphism for flexible programming for example if you‘re making software for a school and you need to print record of different type person which may be student, teacher, non teaching staff etc. So without using polymorphism you need to make separate function for each person related record printing. which lead to increase complexity. In polymorphism you need to make a function for printing record and rewrite different version of this as printing which are isolated by function signature. So you have a better approach to solve your need with flexible coding.

Polymorphism Category



  • Parametric polymorphism is also known as compile-time polymorphism.
  • Subtype polymorphism is also known as runtime polymorphism.
  • Ad-hoc polymorphism is also known as overloading (function and operator).
  • Coercion is also known as (implicit or explicit) casting.


Compile Time Polymorphism


Compile time polymorphism one usually means the fact that you can have a several functions with the same name and the compiler will choose at compile time which one to use depending on the arguments. Compiler binds the related function by calling argument. Selection of function version is based on calling argument, which is done at compile time

#include<iostream.h>
#include<string.h>
class compilepoly
{
     public:
     int add(int x,int y)
     {
           return x+y;
     }
     float add(float x,float y)
     {
           return(x+y);
     }
     int add(int x)
     {
           return x+x;
     }
     char* add(char *a,char *b)
     {
           char *tmp;
           strcpy(tmp,a);
           strcat(tmp,b);
           return tmp;
     }
};
void main()
{
     compilepoly ob;
     cout<<"\nInteger Add\t"<<ob.add(5,5);
     cout<<"\nFloat Add\t"<<ob.add(5.5f,5.5f);
     cout<<"\nString Concat\t"<<ob.add("Girfa : ","Student Help");
}



Run Time Polymorphism


Run time polymorphism is achieved through  which object method invokes during Runtime instead of compile time. For instance, you have 2 class named One and Two derived from a base Class Number. And you create a pointer to the class Number and make it point to any of the derived class object. And when you call a method using base class pointer, the corresponding derived class method is called instead of base class method( You need to put the base class method as virtual for this to work).

class Number
{
     public:
     void info()
     {
           cout<<"\nInside of Number Class";
     }
};
class One : public Number
{
     public:
     void info()
     {
           cout<<"\nInside of Class One";
     }

};
class Two : public Number
{
     public:
     void info()
     {
           cout<<"\nInside of class Two";
     }
};
void main()
{
     Number *base;
     One ob1;
     Two ob2;
     base=&ob1;
     base->info();
     base=&ob2;
     base->info();

}


In mention example base is pointer of Number class which is base class

Number *base;


Next line base class point derive class object

base=&ob1;

So when an attempt has been invoke to call info fucnction then it call base class version of info because base class can indentified member only its own class. So output will be genearate from base class info function in both call.

For make to call drive class function then you need to make base class function info as virtual

class Number
{
     public:
     virtual void info()
     {
           cout<<"\nInside of Number Class";
     }
};



Compile time Polymorphism
Run time Polymorphism
In Compile time Polymorphism, call is resolved by the compiler.
In Run time Polymorphism, call is not resolved by the compiler.
It is also known as Static binding, Early binding and overloading as well.
It is also known as Dynamic binding, Late binding and overriding as well.
Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type.
Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.
It is achieved by function overloading and operatoroverloading.
It is achieved by virtual functions and pointers.
It provides fast execution because known early at compile time.
It provides slow execution as compare to early binding because it is known at runtime.
Compile time polymorphism is less flexible as all things execute at compile time.
Run time polymorphism is more flexible as all things execute at run time.

Ad-hoc polymorphism

Ad-hoc polymorphism allows functions with the same name act differently for each type. This type of polymorphism is achieved through function and operator overload.

Operator Overload

class opload
{
     private:
     int x,y;
     public:
     opload() //Defualt Constructor
     {
           x=y=0;
     }
     opload(int a,int b)
     {
           x=a;
           y=b;
     }
     opload operator +(opload ob) //Oveload function
     {
           opload tmp;
           tmp.x=x+ob.x;
           tmp.y=y+ob.y;
           return tmp;
     }
     void show()
     {
           cout<<"X="<<x<<"\tY="<<y<<endl;
     }
};
void main()
{
     opload ob1(10,20),ob2(30,40);
     opload ob3;
     ob3=ob1+ob2;
     ob1.show();
     ob2.show();
}

Coercion Polymorphism (Casting

Coercion happens when an object or a primitive is cast into another object type or primitive type. For example

class A {
 int foo;
public:
 A(int ffoo) : foo(ffoo) {}
 void giggidy() { std::cout << foo << std::endl; }
};

void moo(A a) {
 a.giggidy();
}