You are on page 1of 9

Concepts OOP

polymorphism
CIT743

Polymorphism is another concept of OOP.


It is often applied on inheritance.
It is the ability of using anoperatoror function in different
ways.
Polymorphism gives operators orfunctions different
meanings depending on the occasion.
Poly, referring to many, signifies many uses of operators
andfunctions.
Polymorphism refers to operations or objects that behave
differently in different contexts.

CIT743

Consider having these three statements in one program;


6 + 10;
Khamis + + Juma;
20.56 + 30.78;
The operator + is used more than once and in each case
compiler uses it in adding integers or floats or combining
the strings.
We therefore overload the use of the operator and this
leads to the concept of polymorphism known as operator
overloading.
When we have a situation where there are more than one
function all have the same prototype (returned data type,
function name and arguments) and each has a different
implementation then we practice another concept of
polymorphism known as function overloading

CIT743

Example:
Design and implement a program that deals with two types of
polygons, rectangle and triangle. User is allowed to specify the
two dimensions width and height of each polygon then
calculate and display area of each.
Solution
Inheritance can be applied as usual;
# include <iostream>
using namespace std;
class Polygon {
protected:
float width, height, area;
public:
void setDimensions (float w, float h) {
width=w;
height=h;
}
CIT743

virtual void calculateArea() {


// this is just a method to show that each polygon needs to
have its own
//method for getting the area
}
float getArea() {
return area;
}
};
class Rectangle: public Polygon {
public:
void calculateArea() {
area=width*height ; }
};
class Triangle: public Polygon {
public:

CIT743

void calculateArea(){
area=width*height/2; }
};
int main () {
Rectangle rect;
Triangle tri;
float w,h;
cout<< Specify width and height of Rectangle:;
cin>>w; cin>> h;
rect.setDimensions(w,h);
cout<< Specify width and height of Triangle:;
cin>>w; cin>> h;
tri.setDimensions(w,h);
rect.calculateArea();
tri.calculateArea();
CIT743

cout << Area of Rectangle is : <<rect.getArea() << endl;


cout << Area of Triangle is : <<tri.getArea() <<endl;
system(pause);
return 0;
}
In this example, according to the question, we dont need
objects of polygon, only of rectangle and triangle.
But any polygon needs a method for calculating the area as
this is a general concept. A common concepts needs to be
represented in the base class.
Therefore we need to get a general definition of the method
in our base class with a general implementation just to show
a pattern of the method implementation and redefine the
same method in the derived classes. By redefining the
method we implement polymorphism.
We can just put a comment as its implementation since we
dont have a general formula for calculation of any polygon.
CIT743

If we dont expect to have objects of a base class, then we


can label the method as virtual which informs compiler that
the methods implementation wont be executed. Then
method will then be known as virtual method.
If the implementation is not useful in terms of execution,
then we can just write a prototype without any
implementation. That method will be called as pure virtual
function
virtual void calculateArea()=0;
With this approach, the definition of the class polygon will
be as follows;
class Polygon {
protected:
float width, height, area;
public:
void setDimensions (float w, float h) {
width=w;
height=h;
}
CIT743

virtual void calculateArea() =0;


float getArea() {
return area;
}
};
In some cases, you might have a base class with all its
methods as virtual ones. This class is also known as virtual
class.
Therefore there are three common types of polymorphism;
function overloading, operator overloading and virtual
functions.
Significance of polymorphism is to provide easy
understanding of the solution by maintaining the original
structure of the base class in derived classes.
Also easy extendibility of the solution having the fact that
the skeleton is there.
Time and effort on coding is highly reduced if pure virtual
functions are used.
Brings flexibility of the program in adopting new behaviors
CIT743
brought by extended derived classes.

You might also like