Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

OBJECT ORIENTED PROGRAMMING

Lab # 09 OOP

MUHAMMAD SAIM ASHRAF

Names

FA21-BEE-128
Registration
Numbers

Class 3A (BEE)

Instructor’s Name DR RIAZ HUSSAIN

Lab Assessment
Post Lab Total
Pre-Lab In-Lab
Data Presentation Data Analysis Writing Style
Lab 10 – Polymorphism
https://onlinegdb.com/aczQYNliF
Task 4.1 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.
Polygon* p = new Polygon(20, 30);

LINK:
https://onlinegdb.com/nloIR2Icc

Task 4.2 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. We can’t make any object of abstract class. This is how we
make pure virtual function.
virtual double getArea() = 0;

LINK:
https://onlinegdb.com/iaxNpkthN

Task 4.3 Example


This example uses pure virtual function in the person class. The person class is an
abstract class and no objects can be created for it. It also adds two derived classes,
student and professor. Student and professor objects are casted into the person
class type through array of pointers.

LINK:
https://onlinegdb.com/XU0vuJ7m7

Task 5.1
In this program we just created a program that derive two classes from class base,
and for each I defined iam() to write out the name of the class. I assigned the
address of objects of the derived classes to base pointers and call iam() through the
pointers. After removing virtual keyword I got following output
Derived class A
Derived class B
base
base

LINK:
https://onlinegdb.com/ejQhQ6Hu-C

Task 5.2
In this program we have to just develop a simple payroll application. In this the
program calculates salary polymorphically and generates report. I made Employee
an abstract class. Declared salary() and display() as pure virtual functions in it.
Derive salaried employee, hourly employee and commissioned employee from base
class Employee

LINK:
https://onlinegdb.com/aM2lzFGME

Task 5.3
In this program I created a base class called shape. Used this class to store two
double type values that could be used to compute the area of figures. Derived two
specific classes called triangle and rectangle from the base shape. Marked the
display_area() as a virtual function and redefine this function in the derived class to
suit their requirements.
void Triangle::display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
void Rectangle::display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}

LINK:
https://onlinegdb.com/QztJgWP3h

Task 6.1
In this program I created a class hierarchy that performs conversions from one
system of units to another. It was simply done by making compute function virtual
in base class and then overrided it for every conversion needed.

LINK:
https://onlinegdb.com/XapQRVVmh

You might also like