Polymorphism Feature in C++

Polymorphism Feature in C++

Polymorphism means single name but multiple formats and this is one of the crucial features of C++. Furthermore, when there are multiple formats of a single function then choosing an appropriate version of the function is a problem.

Choosing appropriate version of the function at the compile time is referred as compile time polymorphism, and at run, time is called runtime polymorphism.

Moreover, to implement both concepts different approaches are used which are numbered below.
1-Compile time polymorphism-> Function overloading and operator overloading.
 2-Run time polymorphism-> Virtual function
Furthermore, in function overloading, there may be more than one functions of the same name with different numbers of arguments and type and different types of return values.

However, compile decide which version of the function would be called at compile time after matching the argument list and its types and return types.
Example of compile time polymorphism
float average( int, int,int);
float average(int, int, int,int);
int main()
{
cout<<” Average of three numbers”<< average(2,3,4);
cout<<“Average of four numbers”<< average(2,3,4,5);
return 0;
}

float average( int a, int b, int c)
{
float av;
av = (a + b + c)/3;
return av;
}
float average( int a, int b, int c, int d)
{
av = ( a + b + c + d)/4;
return 0;
}
In the above implementation, a function average which has has two versions one with four arguments and another with three arguments and compiler calls them with matching arguments in main() function. To implement run time polymorphism, concept of virtual function and pointer to class will be used and it is better to see example to understand.

Example of runtime polymorphism program.

class PrintBase
{
int a;
public:
virtual void print() {};
};

class PrintDerived1 : PrintBase
{
int b;
public:
void print();
};

class PrintDerived2 : PrintBase
{
int c;
public:
void print();
};

void PrintDerived1 : print()
{
cout<<” This is derived class PrintDerived1  print”;
};
void PrintDerived2 : print()
{
cout<<” This is derived class PrintDerived2  print”;
};

int main()
{
PrintBase *pb[2];
PrintDerived1 *pd1;
PrintDerived2 *pd2;
pb[0]=&pd1;
pb[1]=&pd2;
pb[0]->print(); // This will call PrintDerived1’s print function.
pb[1]->print(); // This will call PrintDerived2’s print function.
return 0;
}

In the above implementation, there is a base class Print Base and inside this class there is a virtual function print(). Which will be implemented in two derived classes PrintDerived1 and PrintDerived2 and in main().

Furthermore, function an array of pointer to class pb is declared and two more pointer to objects pb1 and pb2 are declared.

And pb[0] holds the address of pd1 and pb[1] holds address of pd2 and at run time pb[0] calls print function of PrintDerived1 and pb[2] calls print function of PrintDerived2 and that is what run time polymorphism.

Conclusion- To sum up, in this article, we have described and implemented polymorphism with the help of very simple examples, hope it will be very useful for you and you will understand basics of polymorphism.

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.