0% found this document useful (0 votes)
20 views9 pages

Abdullah Farooq F2023266160 Lab Manual 14

Uploaded by

hifadi1393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Abdullah Farooq F2023266160 Lab Manual 14

Uploaded by

hifadi1393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LabManual:Object Oriented Programming

University of Management and


Technology, Lahore Campus

Lab- 14 Manual
Lab Instructor: Riaz Ahmad
Department of Computer Science
Email: [email protected]
Question 1:
In this task, you have to develop a simple payroll application. In this company there are three
kinds of employees: i) salaried employee, ii) hourly employee, and iii) commissioned employee.
Derive salaried employee (have fixed monthly salary), hourly employee (salary computed on
hourly basis), and commissioned employee (salary is based on commission on each sale) from
base class Employee.
Formula to compute NetSalary: salary-taxRate
For example:
computedSalary = 35000, taxRate = 5%
35000*5/100 = 1750
NetSalary = 35000-1750= 33250

#include<iostream>
using namespace std;

class Employee {
protected:

DepartmentofComputer Science,UMT,Lahore.1Riaz Ahmad


LabManual:Object Oriented Programming
string name;
double taxRate;
public:
Employee(string n, double tRate) {
name = n;
taxRate = tRate;
}
string getName() {
return name;
}
virtual void calcNetSalary() {
// Empty base implementation
}
};

class SalariedEmp : public Employee {


private:
double monthlySalary;
public:
SalariedEmp(double mSal, string n, double tRate) : Employee(n, tRate) {
monthlySalary = mSal;
}
void calcNetSalary() override {
double netSalary = monthlySalary - (monthlySalary * taxRate / 100);
cout << "\n" << name << " " << netSalary;
}
};

class HourlyEmp : public Employee {


private:
int hours;
double hourlyRate, salary;
public:
HourlyEmp(int hrs, double rate, string n, double tRate) : Employee(n, tRate) {
hours = hrs;
hourlyRate = rate;
}
void calcNetSalary() override {
salary = hours * hourlyRate;
double netSalary = salary - (salary * taxRate / 100);
cout << "\n" << name << " " << netSalary;
}
};

class CommEmp : public Employee {


private:
int sales;
double commRate, salary;
public:
CommEmp(int s, double cr, string n, double tRate) : Employee(n, tRate) {

DepartmentofComputer Science,UMT,Lahore.2Riaz Ahmad


LabManual:Object Oriented Programming
sales = s;
commRate = cr;
}
void calcNetSalary() override {
salary = sales * 11000 * commRate;
double netSalary = salary - (salary * taxRate / 100);
cout << "\n" << name << " " << netSalary;
}
};

int main() {
cout << "NAME" << " " << "Net Salary\n";
SalariedEmp sE(55000.00, "Abdullah Farooq", 5.0);
sE.calcNetSalary();
HourlyEmp hE(11, 8000, "Ali", 5.0);
hE.calcNetSalary();
CommEmp cE(17, 0.09, "Zeeshan", 5.0);
cE.calcNetSalary();
return 0;
}

For commissioned Employee, salary will be calculated as: sales*11000*commRate. Here11,000


is the static price of one sold article.

Output Format:
Name Net Salary
Aamir 14250
Fakhir 7520
Fuaad 14400 ……

Question 2:
Implement a shape hierarchy in this task. You must have your super class shape and 2
subclasses two-dimensional shape and three-dimensional shape. Under two-dimensional
shape, you have other subclasses circle and square. Under the three-dimensional shape you

DepartmentofComputer Science,UMT,Lahore.3Riaz Ahmad


LabManual:Object Oriented Programming
have the sphere and cube.
Each two-dimensional shape should contain a method getArea to calculate the area of the two-
dimensional shape. Each three-dimensional shape should have
methods getArea and getVolume to calculate the surface area and volume, of the three-
dimensional shape.
#include<iostream>
using namespace std;

class Shape {
public:
virtual double getArea() {
return 0;
}
};

class TwoDimensionalShape : public Shape {


public:
double getArea() override {
return 0;
}
};

class ThreeDimensionalShape : public Shape {


public:
double getArea() override {
return 0;
}

virtual double getVolume() {


return 0;
}
};

class Circle : public TwoDimensionalShape {


private:
double radius;
public:
Circle(double r) {
radius = r;
}

double getArea() override {


return 3.14 * radius * radius;
}
};

class Square : public TwoDimensionalShape {


private:
double side;
public:

DepartmentofComputer Science,UMT,Lahore.4Riaz Ahmad


LabManual:Object Oriented Programming
Square(double s) {
side = s;
}

double getArea() override {


return side * side;
}
};

class Sphere : public ThreeDimensionalShape {


private:
double radius;
public:
Sphere(double r) {
radius = r;
}

double getArea() override {


return 4 * 3.14 * radius * radius;
}

double getVolume() override {


return (4.0 / 3.0) * 3.14 * radius * radius * radius;
}
};

class Cube : public ThreeDimensionalShape {


private:
double side;
public:
Cube(double s) {
side = s;
}

double getArea() override {


return 6 * side * side;
}

double getVolume() override {


return side * side * side;
}
};

int main() {
Circle c(8.5);
cout << "Area of circle: " << c.getArea() << endl;

Square s(7.5);
cout << "Area of square: " << s.getArea() << endl;

DepartmentofComputer Science,UMT,Lahore.5Riaz Ahmad


LabManual:Object Oriented Programming
Sphere sp(6.9);
cout << "Area of sphere: " << sp.getArea() << endl;
cout << "Volume of sphere: " << sp.getVolume() << endl;

Cube cb(4.7);
cout << "Area of cube: " << cb.getArea() << endl;
cout << "Volume of cube: " << cb.getVolume() << endl;

return 0;
}

Question 3:
1. ADT:NumDays
Design aclasscalled NumDays. Theclass’spurposeisto storeavaluethat
representsanumberofworkhoursandconvert it to anumberofdays.
Forexample,8 hours wouldbeconvertedto1 day, 12hours wouldbe convertedto1.5 days,and18
hours wouldbeconvertedto2.25days. The class should have a constructor that accepts a
number of hours, as well as member functions for storing and retrieving thehoursanddays.
Theclassshouldalsohavethefollowingoverloadedoperators:
Additionoperator(+):WhentwoNumDaysobjectsareaddedtogether,theoverloaded+operatorsh
ouldreturnthesumof thetwoobjects’hoursmembers.
Subtractionoperator(–):WhenoneNumDaysobjectissubtractedfromanother,theoverloaded–
operatorshouldreturnthedifferenceof thetwoobjects’hoursmembers.
Prefixandpostfixincrementoperators(+
+):Theseoperatorsshouldincrementthenumberofhoursstoredintheobject.Whenincremented,th
enumberof days should beautomaticallyrecalculated.
Prefixandpostfixdecrementoperators(--):Theseoperatorsshoulddecrementthenumberofhoursst
oredintheobject.When decremented,thenumberofdays should beautomaticallyrecalculated.

ADT:TimeOff
DesignaclassnamedTimeOff.Thepurposeoftheclassistotrackanemployee’ssickleave,vacation,
andunpaidtimeoff.Itshouldhave,asmembers,the followinginstances of theNumDaysclass:

DepartmentofComputer Science,UMT,Lahore.6Riaz Ahmad


LabManual:Object Oriented Programming
maxSickDays A NumDays object that records the maximum number of days of sick leave
the employee may take.sickTaken A NumDays object that records the number of days of
sick leave the employee has already taken.maxVacation A NumDays object that records the
maximum number of days of paid vacation the employee may take.vacTaken A NumDays
object that records the number of days of paid vacation the employee has already
taken.maxUnpaid

ANumDaysobjectthatrecordsthemaximumnumberofdaysofunpaidvacationtheemployeemayt
ake.unpaidTaken ANumDaysobjectthatrecordsthenumberofdays
ofunpaidleavetheemployeehastaken.
Additionally,theclassshouldhavemembersforholdingtheemployee’snameandidentificationnu
mber.Itshouldhaveanappropriateconstructorandmemberfunctionsforstoringandretrievingdatai
nanyofthememberobjects.
InputValidation:Companypolicystatesthatanemployeemaynotaccumulatemorethan240hourso
fpaidvacation.Theclassshouldnotallow themaxVacationobjecttostoreavalue greaterthan this
amount.

PersonnelReport:ADriverProgram
WriteaprogramthatusesaninstanceoftheTimeOffclassyoudesignedaboveandperformthefollow
ing.
Theprogramshouldasktheusertoenterthenumberofmonthsanemployeehasworkedforthecompa
ny.
Itshould
thenusetheTimeOffobjecttocalculateanddisplaytheemployee’smaximumnumberofsickleavea
ndvacationdays.
Note:Employeesearn12hoursofvacationleaveand8hoursofsickleavepermonth.1

#include <iostream>
using namespace std;
class NumDays {
private:
double hours;
public:
NumDays(double h = 0.0) {
hours = h;
}
void setHours(double h) {
hours = h;
}
double getHours() {
return hours;
}
double getDays() {
return hours / 8.0;
}
NumDays operator+(NumDays &obj) {
NumDays temp;

DepartmentofComputer Science,UMT,Lahore.7Riaz Ahmad


LabManual:Object Oriented Programming
temp.hours = hours + obj.hours;
return temp;
}
NumDays operator-(NumDays &obj) {
NumDays temp;
temp.hours = hours - obj.hours;
return temp;
}
NumDays operator++() {
++hours;
return *this;
}
NumDays operator++(int) {
NumDays temp = *this;
hours++;
return temp;
}
NumDays operator--() {
--hours;
return *this;
}
NumDays operator--(int) {
NumDays temp = *this;
hours--;
return temp;
}
};
int main() {
NumDays nd1(18);
NumDays nd2(12);
cout << "NumDays 1: " << nd1.getHours() << " hours = " << nd1.getDays() << " days" << endl;
cout << "NumDays 2: " << nd2.getHours() << " hours = " << nd2.getDays() << " days" << endl;
NumDays nd3 = nd1 + nd2;
cout << "NumDays 3 (nd1 + nd2): " << nd3.getHours() << " hours = " << nd3.getDays() << "
days" << endl;
NumDays nd4 = nd1 - nd2;
cout << "NumDays 4 (nd1 - nd2): " << nd4.getHours() << " hours = " << nd4.getDays() << "
days" << endl;
++nd1;
cout << "After prefix increment, NumDays 1: " << nd1.getHours() << " hours = " <<
nd1.getDays() << " days" << endl;
nd1++;
cout << "After postfix increment, NumDays 1: " << nd1.getHours() << " hours = " <<
nd1.getDays() << " days" << endl;
--nd1;
cout << "After prefix decrement, NumDays 1: " << nd1.getHours() << " hours = " <<
nd1.getDays() << " days" << endl;
nd1--;
cout << "After postfix decrement, NumDays 1: " << nd1.getHours() << " hours = " <<
nd1.getDays() << " days" << endl;

DepartmentofComputer Science,UMT,Lahore.8Riaz Ahmad


LabManual:Object Oriented Programming
return 0;
}

DepartmentofComputer Science,UMT,Lahore.9Riaz Ahmad

You might also like