Polymorphism Assignment Help

Assignment Help: >> Introduction to Object-Oriented Programming - Polymorphism

Polymorphism

Polymorphism is another important OOP concept.  A Polymorphism denotes the ability to take more than one form. For instance, an operation might exhibit various behaviors in various instances. The behaviour depends upon the category of data used in the operation.  For example, let consider the operation of addition. For two numbers, the operation will produce a sum. The operation would produce a third string by concatenation if the operands were strings. Figure describes in which a single function name can be used to handle various number and various types of arguments.  This is something same to a particular word having many different meanings depending on the context.

91_polymorphism.png

Figure: Polymorphism

Polymorphism plays a very important role to permitting objects having different internal structures to share the similar external interface.  This means that a common class of operations might be accessed in the similar manner even by specific actions related with every operation might differ. Polymorphism is extensively used in implementing inheritance.

Example:

//sample program for polymorphism

//source file: poly.cpp

# include <iostream.h>

int abs(int I);

long abs (long l);

double abs(double d);

main()

{

cout<<abs(-10)<<"\n";

cout<<abs(10L)<<"\n";

cout<<abs(-12.35)<<"\n";

return 0;

}

int abs(int i)

{

cout<<"using int method";

return i>0?i:-i;

}

long abs(long l)

{

cout<<"using long method";

return l>0?l:-l;

}

double abs(double d)

{

cout<<"using double method";

return d>0?d:-d;

}

The given example proves the concept of polymorphism, that is the function abs () is similar, but it acts differently in different places depending upon the argument we pass.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd