Introduction The purpose of this assignment is to understand inheritance when wo
ID: 3577331 • Letter: I
Question
Introduction
The purpose of this assignment is to understand inheritance when working with C++ objects.
Consider the following inheritance hierarchy.
Employee class (Code COMPLETED)
The base class, Employee, contains 2 private variables, empId and empName. There is a class constructor that takes in parameters used to populate the class private variables. In order to keep the example short, mutator and accessor functions are only provided for the empName field, The contains a toString function that returns a string representation of the class private variables.
SalariedEmployee class (Code COMPLETED)
This class is derived from the Employee class and contains private variables that hold salary and bonus. Accessor and mutator functions are provided for each of these variables. In addition there is a function named getWages that returns the sum of salary and bonus. The toString function overrides the toString function of the base class. It retrieves the information returned by the toString function of the base class and pads on its private variables before returning it.
HourlyEmployee (INCOMPLETE)
This derived class keeps track of hours worked and the hourly rate. It contains accessor and mutator functions for each of these variables, along with an overloaded constructor.
This class computes wages as follows:
Regular wages for the first 40 hours of work (hours worked * hourly wage)
Overtime wages for hours worked over 40 @ 1.5 times hourly rate for hours over 40
There are functions that compute regular wages, overtime wages and total wages. These functions need to be completed by you.
The toString function returns values held by the base class, along with values held by this class.
Lab 7 incomplete files
Employee class (Complete code provided)
Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
/*
This is the base class of the inheritance hierarchy
In order to keep the example simple, I have only
provided accessor and mutator functions for the
empname variable
*/
class Employee
{
private:
string empname = "";
int empId=0;
public:
Employee(string = "", int = 0);
void setEmpname(string);
string getEmpname();
string toString();
};
Employee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Employee.h"
Employee::Employee(string _empname, int _empid)
{
empname = _empname;
empId = _empid;
}
void Employee::setEmpname(string _empname)
{
empname = _empname;
}
string Employee::getEmpname()
{
return empname;
}
string Employee::toString()
{
ostringstream ostr;
ostr << "Employee Name : " << empname <<
" Employee ID : " << empId;
return ostr.str();
}
SalariedEmployee class (Complete code provided)
SalariedEmployee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
#include "Employee.h"
/*
This class is derived from the Employee class
It contains 2 class variables - salary and bonus
accessor and mutator functions are provided for both of them
*/
class SalariedEmployee : public Employee
{
private:
double salary = 0;
double bonus = 0;
public:
SalariedEmployee(string = "", int = 0, double = 0, double = 0);
void setSalary(double);
void setBonus(double);
double getSalary();
double getBonus();
double getWages();
string toString();
};
SalariedEmployee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "SalariedEmployee.h"
SalariedEmployee::SalariedEmployee(string _empname, int _empId,
double _salary, double _bonus)
: Employee(_empname, _empId)
{
salary = bonus = 0;
setSalary(_salary);
setBonus(_bonus);
}
void SalariedEmployee::setSalary(double _salary)
{
if (_salary > 0)
salary = _salary;
else
cout << "Salary cannot be negative ";
}
void SalariedEmployee::setBonus(double _bonus)
{
if (_bonus > 0)
bonus = _bonus;
else
cout << "Bonus cannot be negative ";
}
double SalariedEmployee::getSalary()
{
return salary;
}
double SalariedEmployee::getBonus()
{
return bonus;
}
double SalariedEmployee::getWages()
{
return salary + bonus;
}
string SalariedEmployee::toString()
{
ostringstream ostr;
ostr << fixed << showpoint << setprecision(2);
ostr << Employee::toString()
<< " Salary : $" << salary
<< " Bonus : $" << bonus
<< " Total Compensation : $" << getWages();
return ostr.str();
}
HourlyEmployee class (Complete code NOT provided)
HourlyEmployee.h (Complete code provided)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
#include "Employee.h"
/*
This class is derived from the Employee class
It contains 2 class variables - hours worked
and hourly rate. Accessor and mutator functions are provided
for both of them
*/
class HourlyEmployee : public Employee
{
private:
int hours = 0;
double hourly_rate = 0;
public:
HourlyEmployee(string = "", int = 0, int = 0, double = 0);
void setHours(int);
void setHourly_rate(double);
int getHours();
double getHourly_rate();
double getRegularWages();
double getOvertimeWages();
double getTotalWages();
string toString();
};
HourlyEmployee.cpp (Complete code NOT provided - you need to complete it!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "HourlyEmployee.h"
HourlyEmployee::HourlyEmployee(string _empname, int _empId,
int _hours, double _hourly_rate)
: Employee(_empname, _empId)
{
hours = hourly_rate = 0;
setHours(_hours);
setHourly_rate(_hourly_rate);
}
void HourlyEmployee::setHours(int _hours)
{
if (_hours >= 0)
hours = _hours;
else
cout << "Hours must be >= 0 ";
}
void HourlyEmployee::setHourly_rate(double _hourly_rate)
{
if (_hourly_rate > 0)
hourly_rate = _hourly_rate;
else
cout << "Hourly Rate must be > 0 ";
}
double HourlyEmployee::getHourly_rate()
{
return hourly_rate;
}
int HourlyEmployee::getHours()
{
return hours;
}
double HourlyEmployee::getRegularWages()
{
double reg_wages = 0;
/*
Write code to compute regular wages
*/
return reg_wages;
}
double HourlyEmployee::getOvertimeWages()
{
double ot_wages = 0;
/*
Write code to compute overtime wages
*/
return ot_wages;
}
double HourlyEmployee::getTotalWages()
{
return getRegularWages() + getOvertimeWages();
}
string HourlyEmployee::toString()
{
ostringstream ostr;
ostr << fixed << showpoint << setprecision(2);
ostr << Employee::toString()
<< " Hours worked : " << hours
<< " Hourly Rate : $" << hourly_rate
<< " Regular Wages : $" << getRegularWages()
<< " Overtime Wages : $" << getOvertimeWages()
<< " Total Wages : $" << getTotalWages();
return ostr.str();
}
Class_tester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
int main()
{
Employee empl1("Maya", 123);
SalariedEmployee salemp1("Pam", 234, 1000, 500);
HourlyEmployee hourlyemp1("Casey", 345, 50, 10);
cout << "Details about empl1: " << empl1.toString() << " ";
cout << "Details about salemp1: " << salemp1.toString() << " ";
cout << "Details about hourlyemp1: " << hourlyemp1.toString() << " ";
return 0;
}
Output from the completed version of the assignment is shown below:
Details about empl1:
Employee Name : Maya
Employee ID : 123
Details about salemp1:
Employee Name : Pam
Employee ID : 234
Salary : $1000.00
Bonus : $500.00
Total Compensation : $1500.00
Details about hourlyemp1:
Employee Name : Casey
Employee ID : 345
Hours worked : 50
Hourly Rate : $10.00
Regular Wages : $400.00
Overtime Wages : $150.00
Total Wages : $550.00
Press any key to continue . . .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
/*
This is the base class of the inheritance hierarchy
In order to keep the example simple, I have only
provided accessor and mutator functions for the
empname variable
*/
class Employee
{
private:
string empname = "";
int empId=0;
public:
Employee(string = "", int = 0);
void setEmpname(string);
string getEmpname();
string toString();
};
Explanation / Answer
#include "HourlyEmployee.h"
HourlyEmployee::HourlyEmployee(string _empname, int _empId,
int _hours, double _hourly_rate)
: Employee(_empname, _empId)
{
hours = hourly_rate = 0;
setHours(_hours);
setHourly_rate(_hourly_rate);
}
void HourlyEmployee::setHours(int _hours)
{
if (_hours >= 0)
hours = _hours;
else
cout << "Hours must be >= 0 ";
}
void HourlyEmployee::setHourly_rate(double _hourly_rate)
{
if (_hourly_rate > 0)
hourly_rate = _hourly_rate;
else
cout << "Hourly Rate must be > 0 ";
}
double HourlyEmployee::getHourly_rate()
{
return hourly_rate;
}
int HourlyEmployee::getHours()
{
return hours;
}
double HourlyEmployee::getRegularWages()
{
double reg_wages = 0;
/*
Write code to compute regular wages
*/
if(getHours() >= 40)
reg_wages = 40.00*getHourly_rate();
else
reg_wages = getHours()*getHourly_rate();
return reg_wages;
}
double HourlyEmployee::getOvertimeWages()
{
double ot_wages = 0;
/*
Write code to compute overtime wages
*/
if(getHours() > 40)
ot_wages = (getHours() - 40.00)*(getHourly_rate()*1.5);
return ot_wages;
}
double HourlyEmployee::getTotalWages()
{
return getRegularWages() + getOvertimeWages();
}
string HourlyEmployee::toString()
{
ostringstream ostr;
ostr << fixed << showpoint << setprecision(2);
ostr << Employee::toString()
<< " Hours worked : " << hours
<< " Hourly Rate : $" << hourly_rate
<< " Regular Wages : $" << getRegularWages()
<< " Overtime Wages : $" << getOvertimeWages()
<< " Total Wages : $" << getTotalWages();
return ostr.str();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.