Lab Report 10
Lab Report 10
Lab Report 10
Lab # 09 OOP
Names
FA21-BEE-128
Registration
Numbers
Class 3A (BEE)
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
LINK:
https://onlinegdb.com/iaxNpkthN
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