OOP Lab 10
OOP Lab 10
EXPERIMENT NO. 10
Lab Title:
Objective:
LAB ASSESSMENT:
Date: Signature:
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
Lab 10
Items Description
Lab Course Title Lab – Object Oriented Programming
Lab Title Polymorphism & Abstract Class.
Objective learn to apply a core concept of OOP in C++ and how to implement
polymorphism using pointers and derived classes
Duration 3 Hours
Operating System/Tools Windows/MS Office/Dev C++/Computer System
Polymorphism:
The word polymorphism means having many forms. A real-life example of polymorphism is a person
who at the same time can have different characteristics. A man at the same time is a father, a husband,
and an employee. So, the same person exhibits different behaviour in different situations. This is called
polymorphism. In C++, polymorphism concept can be applied to functions and operators. A single
function can work differently in different situations. Similarly, an operator works different when used
in different context.
Polymorphism allows you to create a pointer to a derived class which is also compatible with
the base class.
Types of Polymorphism
Polymorphism in C++ can be classified into two types:
Compile-time Polymorphism
Runtime Polymorphism
1
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;
void info() {
cout << "This is a shape." << endl;
}
};
int main() {
// Shape s; // Error: Cannot instantiate abstract
class
Shape* shapePtr;
Circle c;
Square s;
shapePtr = &c;
shapePtr->draw(); // Output: Drawing a
circle.
shapePtr = &s;
shapePtr->draw(); // Output: Drawing a
square.
return 0;
}
2
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
Runtime Polymorphism:
Also known as late binding and dynamic polymorphism, the function call in runtime
polymorphism is resolved at runtime in contrast with compile time polymorphism, where the compiler
determines which function call to bind at compilation. Runtime polymorphism is implemented using
function overriding with virtual functions.
1. Function Overriding
Function Overriding occurs when a derived class defines one or more member functions of the base
class. That base function is said to be overridden. The base class function must be declared as virtual
function for runtime polymorphism to happen.
Example:
In the below example, a virtual function display() is defined in the base class Base, and it is overridden
in the derived class Derived. The Base class pointer basePtr points to an object of the Derived class.
When the display() function is called using the basePtr, the derived class version of
the display() function is called, and prints "Derived class function." This is possible because call is
resolved at the runtime.
class Base {
public:
// Virtual function
virtual void display() {
cout << "Base class function";
}
};
int main() {
3
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
Derived derivedObj;
2. Virtual Function:
A virtual function is a member function which is declared in the base class using the keyword
virtual and is redefined (Overriding) by the derived class. The term Polymorphism means the
ability to take many forms. It occurs if there is a hierarchy of classes which are all related to
each other by inheritance
Example:
This example demonstrates a polygon-rectangle hierarchy with the use of virtual functions in the base
class and how Rectangle objects are being casted into the Base class type through pointers.
}
virtual double getArea() //virtual function
{ return 0;
}
virtual string gettype() //virtual function
{ return "Generic Polygon";
}};
class rectangle: public polygon //derived class
{
4
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
public:
rectangle(double w, double h): polygon(w,h)
//two argument constructor
{
}
double getArea() //function to return value of
area
{
return (getwidth() * getheight());
}
string gettype() //type function for rectangle
{ return "rectangle";
}
};
int main()
{
polygon * p = new polygon(20,30); //pointer
declare and initialize for polygon
rectangle * r = new rectangle(20,30); //pointer
declare and initialize for rectangle
cout<<p->gettype()<<endl<<" Area : "<<p-
>getArea()<<endl; //call funtions
cout<<r->gettype()<<endl<<" Area : "<<r-
>getArea()<<endl;
cout<<endl<<endl;
}
Example:
This example demonstrates the polygon-rectangle hierarchy with pure virtual functions. The base class
now is an abstract base class and lots of other classes added to the hierarchy.
Source Code Output
5
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
}
string gettype() //type function for rectangle
{ return "rectangle";
}};
6
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
int main()
{ polygon * ppoly[100] = {new
rectangle(1.2,2.1),new square(2.5), new
triangle(5.0,6.0), new parallelogram(20,30),new
rectangle(100,200),
new square(2.5), new triangle(5.0,6.0),
new parallelogram(20,30), NULL}; //pointer
declare and initialize
for(int i=0; ppoly[i]!=NULL; i++) //for loop is
used to display all data
{ cout<<i+1<<" ."<<ppoly[i]-
>gettype()<<endl<<"\n\t Width = "<<ppoly[i]-
>getwidth()<<"\n\t Height = "<<ppoly[i]-
>getheight()<<"\n\t Area = "<<ppoly[i]-
>getArea();
cout<<endl;
}
}
In Lab Tasks
In Lab Task 01: You are developing a payment processing system for an online marketplace. The
system must support multiple payment methods: Credit Card, PayPal, and Bank Transfer. Each
payment method has a different way of processing a payment, but all of them should follow a common
interface. Design this system using abstract classes.
What is the common functionality all payment types must implement?
How would you design the abstract base class?
How will polymorphism help in processing payments uniformly?
7
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering
Lab Task 02: You are developing a drawing application that allows users to draw different shapes
like Circle, Rectangle, and Triangle. Each shape must implement functions to calculate its area and
draw itself on the screen. Design a class structure using an abstract class that defines the common
interface for all shapes.
What are the common behaviors shared by all shapes?
Why use an abstract class instead of a concrete base class?
How would polymorphism help in managing different shapes?