Homework 4 The objective of this homework is to give you experience using inheri
ID: 3865082 • Letter: H
Question
Homework 4
The objective of this homework is to give you experience using inheritance with C++. You will use the provided employee base class and implement two derived classes, salaried employee and hourly employee. This assignment will be used for a follow-on assignment.
For this assignment, you will use the base class Employee, which is implemented with the files Employee.h and Employee.cpp. A test file, test.cpp, is also provided for your use, as you choose to use it.
Each employee object has a user id; a first and last name; a middle initial; and a department code (integer). Your implementation must now add two derived classes. The first one will be for a salaried employee, which will require a monthly salary variable. You will need member functions including a constructor, set and get salary functions, a salary calculation function, and a print function. For versatility, you might include a variable that specifies what fraction of time the person worked and use the fraction in the salary calculation. Hint: set the fraction default to one. For consistency, name your salaried employee class as SalariedEmployee
Your second class should represent an hourly worker. In this case you will need to store the hours worked and the hourly rate. You should also include provisions for overtime hours, which will be paid at 1.5 times the regular hourly rate. Hint: set the default overtime hours to 0. You will need similar functions as the salaried employee to set and get variables, as well as to calculate salary. Name your hourly employee class as HourlyEmployee.
Generate a test file that includes at least two of each type of worker to test the classes for proper operation. I recommend a full-time and half-time salaried worker and two hourly worker test cases, one of which earns overtime.
Execute your test program and copy the outputs to a text file to demonstrate proper execution.
What to submit:
Please submit a copy of your source code files and a text file(s) that includes execution output that demonstrates proper operation.
Use these files
I am using NetBeans
EMPLOYEE.CPP FILE:
/* employee.cpp*/
#include <iostream>
#include <iomanip>
#include <string>
#include "Employee.h"
using namespace std;
// constructor
Employee::Employee(long id, const string &last, const string &first, const string &initial,
int dept)
{
myIdNum = id;
myLastName = last;
myFirstName = first;
myMiddleInitial = initial;
myDeptCode = dept;
}
// Accessor function defintions
void Employee::setIdNum (const long id)
{
myIdNum = id;
}
long Employee:: getIdNum () const // get id number
{
return myIdNum;
}
void Employee:: setLastName (const string &last) // set last name
{
myLastName = last;
}
string Employee:: getLastName () const // return last name
{
return myLastName;
EMPLOYEE.H HEADER FILE:
/* employee.h */
#ifndef EMPLOYEE
#define EMPLOYEE
#include <string>
using namespace std;
class Employee
{
public:
Employee(long = 0, const string & ="" , const string & ="", const string & = "", int =0); // constructor
void setIdNum (const long ); // set id number
long getIdNum () const; // get id number
void setLastName (const string &); // set last name
string getLastName () const; // return last name
void setFirstName (const string &); // set first name
string getFirstName () const; // return first name
void setMiddleInitial (const string &); // set middle initial
string getMiddleInitial () const; // return set middle initial
void setDeptCode(const int); // set department code
int getDeptCode () const; // get department code
void printEmployee (); // print Employee information
private:
long myIdNum; //Employee id number
string myLastName; //Employee last name
string myFirstName; //Employee first name
string myMiddleInitial; //Employee middle intial
int myDeptCode; //Department code
};
#endif
TEST.CPP FILE:
/* File: test.cpp */
// File to test the basic employee class
#include<iostream>
#include <string>
#include "Employee.h"
using namespace std;
int main()
{
Employee
e1 (001, "Jones", "Booker", "T", 22),
e2 (002, "Hendrix", "Jimi", "NMI ", 14),
e3 (003, "Morrison", "Jim", "D", 03);
e1.printEmployee();
e2.printEmployee();
}
Explanation / Answer
#ifndef SALARIEDEMPLOYEE
#define SALARIEDEMPLOYEE
#include "Employee.h"
class SalariedEmployee : public Employee
{
public:
SalariedEmployee(long = 0, const string & ="" , const string & ="", const string & = "", int =0, double = 0);
void setSalary (const double ); // set salary
double getSalary () const; // get salary
void setWorkFraction (const double ); // set fraction of work time
double getWorkFraction () const;
double calculateSalary();
void printEmployee (); // print Employee information
private:
double mySalary;
double workFraction;
};
#endif
/* SalariedEmployee.cpp*/
#include <iostream>
#include <iomanip>
#include <string>
#include "SalariedEmployee.h"
using namespace std;
// constructor
SalariedEmployee::SalariedEmployee(long id, const string &last, const string &first, const string &initial, int dept, double salary)
{
Employee::setIdNum(id);
Employee::setLastName(last);
Employee::setFirstName(first);
Employee::setMiddleInitial(initial);
Employee::setDeptCode(dept);
mySalary = salary;
workFraction = 1;
}
void SalariedEmployee::setSalary (const double salary)
{
mySalary = salary;
}
double SalariedEmployee::getSalary() const
{
return mySalary;
}
void SalariedEmployee::setWorkFraction(const double fraction)
{
workFraction = fraction;
}
double SalariedEmployee::getWorkFraction() const
{
return workFraction;
}
double SalariedEmployee::calculateSalary()
{
return mySalary * workFraction;
}
void SalariedEmployee::printEmployee () // print Employee information
{
cout << "SalariedEmployee [Id =" << Employee::getIdNum() << ", lastName = " <<Employee::getLastName() <<", firstName = "<<Employee::getFirstName() << ", middleInitial = "<< Employee::getMiddleInitial() << ", deptCode = "<< Employee::getDeptCode() << " netSalary "<< calculateSalary()<<" "<<endl;
}
#ifndef HOURLYEMPLOYEE
#define HOURLYEMPLOYEE
#include "Employee.h"
class HourlyEmployee : public Employee
{
public:
HourlyEmployee(long = 0, const string & ="" , const string & ="", const string & = "", int =0, int = 0, double = 0);
void setRegularHours (const int );
int getRegularHours () const;
void setOverTimeHours (const int );
int getOverTimeHours () const;
void setHourlyRate (const double );
double getHourlyRate () const;
double calculateSalary();
void printEmployee (); // print Employee information
private:
int regularHours;
int overTimeHours;
double hourlyRate;
};
#endif
/* HourlyEmployee.cpp*/
#include <iostream>
#include <iomanip>
#include <string>
#include "HourlyEmployee.h"
using namespace std;
// constructor
HourlyEmployee::HourlyEmployee(long id, const string &last, const string &first, const string &initial, int dept, int hours, double rate)
{
Employee::setIdNum(id);
Employee::setLastName(last);
Employee::setFirstName(first);
Employee::setMiddleInitial(initial);
Employee::setDeptCode(dept);
regularHours = hours;
hourlyRate = rate;
overTimeHours = 0;
}
void HourlyEmployee::setRegularHours (const int hours)
{
regularHours = hours;
}
int HourlyEmployee::getRegularHours() const
{
return regularHours;
}
void HourlyEmployee::setOverTimeHours(const int hours)
{
overTimeHours = hours;
}
int HourlyEmployee::getOverTimeHours() const
{
return overTimeHours;
}
void HourlyEmployee::setHourlyRate(const double hours)
{
hourlyRate = hours;
}
double HourlyEmployee::getHourlyRate() const
{
return hourlyRate;
}
double HourlyEmployee::calculateSalary()
{
return (regularHours * hourlyRate) + (overTimeHours * hourlyRate * 1.5);
}
void HourlyEmployee::printEmployee () // print Employee information
{
cout << "HourlyEmployee [Id =" << Employee::getIdNum() << ", lastName = " <<Employee::getLastName() <<", firstName = "<<Employee::getFirstName() << ", middleInitial = "<< Employee::getMiddleInitial() << ", deptCode = "<< Employee::getDeptCode() << ", regularHours = "<< getRegularHours() << ", overTimeHours = "<< getOverTimeHours() << ", Net-Salary "<< calculateSalary()<<" "<<endl;
}
/* employee.h */
#ifndef EMPLOYEE
#define EMPLOYEE
#include <string>
using namespace std;
class Employee
{
public:
Employee(long = 0, const string & ="" , const string & ="", const string & = "", int =0); // constructor
void setIdNum (const long ); // set id number
long getIdNum () const; // get id number
void setLastName (const string &); // set last name
string getLastName () const; // return last name
void setFirstName (const string &); // set first name
string getFirstName () const; // return first name
void setMiddleInitial (const string &); // set middle initial
string getMiddleInitial () const; // return set middle initial
void setDeptCode(const int); // set department code
int getDeptCode () const; // get department code
void printEmployee (); // print Employee information
private:
long myIdNum; //Employee id number
string myLastName; //Employee last name
string myFirstName; //Employee first name
string myMiddleInitial; //Employee middle intial
int myDeptCode; //Department code
};
#endif
/* employee.cpp*/
#include <iostream>
#include <iomanip>
#include <string>
#include "Employee.h"
using namespace std;
// constructor
Employee::Employee(long id, const string &last, const string &first, const string &initial, int dept)
{
myIdNum = id;
myLastName = last;
myFirstName = first;
myMiddleInitial = initial;
myDeptCode = dept;
}
// Accessor function defintions
void Employee::setIdNum (const long id)
{
myIdNum = id;
}
long Employee::getIdNum () const // get id number
{
return myIdNum;
}
void Employee::setLastName (const string &last) // set last name
{
myLastName = last;
}
string Employee::getLastName () const // return last name
{
return myLastName;
}
void Employee::setFirstName (const string &first) // set first name
{
myFirstName = first;
}
string Employee::getFirstName () const // return first name
{
return myFirstName;
}
void Employee::setMiddleInitial (const string &middle) // set middle name
{
myMiddleInitial = middle;
}
string Employee::getMiddleInitial () const // return middle name
{
return myMiddleInitial;
}
void Employee::setDeptCode (const int code) // set department code
{
myDeptCode = code;
}
int Employee::getDeptCode () const // get department code
{
return myDeptCode;
}
void Employee::printEmployee () // print Employee information
{
cout << "[Id =" << myIdNum << ", lastName = " <<myLastName <<", firstName = "<< myFirstName << ", middleInitial = "<< myMiddleInitial << ", deptCode = "<< myDeptCode <<" "<<endl;
}
// File to test the basic employee class
#include<iostream>
#include <string>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
using namespace std;
int main()
{
Employee
e1 (001, "Jones", "Booker", "T", 22),
e2 (002, "Hendrix", "Jimi", "NMI ", 14),
e3 (003, "Morrison", "Jim", "D", 03);
SalariedEmployee
e4 (004, "Zubaeyr", "Odin", "T", 03, 60000.0);
SalariedEmployee
e5 (005, "Azhar", "Odin", "D", 03, 50000.0);
e5.setWorkFraction(0.8);
HourlyEmployee
e6 (004, "Will", "Smith", "NMI", 04, 210, 50.7);
HourlyEmployee
e7 (005, "Sumair", "Khan", "D", 036, 300, 70.6);
e7.setOverTimeHours(100);
e1.printEmployee();
e2.printEmployee();
e3.printEmployee();
e4.printEmployee();
e5.printEmployee();
e6.printEmployee();
e7.printEmployee();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.