In: Computer Science
Part I: The Employee Class
You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
* Update the function display() to be a virtual function
* Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects.
Part II: The Derived Classes
Add an weeklyEarning() function to each of the three derived classes. A salaried employee’s weekly earning is as stored. An hourly employee’s weekly earning is the hrsworked * payperhr. A commissioned employee’s weekly earning would be the base + the comAmount.
Employee.h ----------------------------------- #ifndef Employee_H #define Employee_H #include using namespace std; class Employee { private: string lastName; string firstName; int idNum; public: Employee(); //default values assigned to all variables Employee(int idNum, string lname, string fname); void setLName(string); void setFName(string); void setIDNum(int); string getLName(); string getFName(); int getIDNum(); void displayName(); void display(); }; #endif ----------------------------------- ----------------------------------- Employee.cpp ----------------------------------- #include #include #include"Employee.h" using namespace std; Employee::Employee() { //default values assigned to all variables lastName = ""; firstName = ""; idNum = 0; } Employee::Employee(int idNum, string lname, string fname) { this->lastName = lname; this->firstName = fname; this->idNum = idNum; } //Sets last name void Employee::setLName(string lname) { this->lastName = lname; } //sets first name void Employee::setFName(string fname) { this->firstName = fname; } //sets id num void Employee::setIDNum(int idnum) { this->idNum = idnum; } //get last name string Employee::getLName() { return lastName; } //get first name string Employee::getFName() { return firstName; } //get id num int Employee::getIDNum() { return idNum; } //display id and last name void Employee::displayName() { cout << endl << "ID Num : " << this->idNum; cout << endl << "Last name : " << this->lastName; } //display employee details void Employee::display() { cout << endl << "ID Number " << this->idNum; cout << endl << "First name : " << this->firstName; cout << endl << "Last name : " << this->lastName; } ----------------------------------- ----------------------------------- Salaried.h ----------------------------------- #ifndef Salaried_H #define Salaried_H #include "Employee.h" #include using namespace std; class Salaried : public Employee{ private: double weeklySal; public: Salaried(); Salaried(int idNum, string lname, string fname); Salaried(int idNum, string lname, string fname, double sal); void setSal(double nsal); double getSal(); void displaySal(); void display(); }; #endif ----------------------------------- ----------------------------------- Salaried.cpp ----------------------------------- #include "Salaried.h" #include using namespace std; Salaried::Salaried():Employee() { this->weeklySal = 0; } Salaried::Salaried(int idNum, string lname, string fname) :Employee(idNum, lname, fname) { this->weeklySal = 0; } Salaried::Salaried(int idNum, string lname, string fname, double sal) : Employee(idNum, lname, fname) { this->weeklySal = sal; } //set salary void Salaried::setSal(double nsal) { this->weeklySal = nsal; } //get salary double Salaried::getSal() { return weeklySal; } //display name, id num, salary void Salaried::displaySal() { cout << endl << "Name " << this->getLName(); cout << endl << "ID Num " << this->getIDNum(); cout << endl << "Weekly salary " << this->weeklySal; cout << endl << "------------------------------"; } //Display employee details void Salaried::display() { Employee::display(); cout << endl << "Weekly salary " << this->weeklySal; cout << endl << "------------------------------"; } ----------------------------------- ----------------------------------- Commission.h ----------------------------------- #ifndef Commission_H #define Commission_H #include #include "Employee.h" using namespace std; class Commission : public Employee{ private: double basesalary; double comAmount; public: Commission(); Commission(int idNum, string lname, string fname); Commission(int idNum, string lname, string fname, double bsal); void setBase(double npay); void setCom(double nhr); double getBase(); double getCom(); void displayBase(); void displayEmp(); }; #endif ----------------------------------- ----------------------------------- Commission.cpp ----------------------------------- #include "Commission.h" #include using namespace std; //sets Commission instance to default intial values Commission::Commission():Employee() { this->basesalary = 0; this->comAmount = 0; } //Sets Commission instance to Commission parameters Commission::Commission(int idNum, string lname, string fname):Employee(idNum, lname, fname) { this->basesalary = 0; this->comAmount = 0; } //Sets Commission instance to Employee plus Commision parameters Commission::Commission(int idNum, string lname, string fname, double bsal):Employee(idNum, lname, fname) { this->comAmount = 0; this->basesalary = bsal; } //Sets base salary void Commission::setBase(double npay) { this->basesalary = npay; } //Set commision salary void Commission::setCom(double nhr) { this->comAmount = nhr; } //Get base salary double Commission::getBase() { return basesalary; } //Get commission salary double Commission::getCom() { return comAmount; } //Display base salary and commision salary void Commission::displayBase() { cout << endl << "Name " << this->getLName(); cout << endl << "ID Num " << this->getIDNum(); cout << endl << "Base salary " << this->basesalary; cout << endl << "------------------------------"; } //Display commission employee details void Commission::displayEmp() { Employee::display(); cout << endl << "Base salary " << this->basesalary; cout << endl << "Commission Amount : " << this->comAmount; cout << endl << "------------------------------"; } ----------------------------------- ----------------------------------- Hourly.h ----------------------------------- #ifndef Hourly_H #define Hourly_H #include #include"Employee.h" using namespace std; class Hourly : public Employee{ private: double payperhr; double hrsworked; public: Hourly(); Hourly(int idNum, string lname, string fname); Hourly(int idNum, string lname, string fname, double newpay); void setPay(double); void setHrs(double nhr); double getPay(); double getHrs(); void displayHrly(); void display(); }; #endif ----------------------------------- ----------------------------------- Hourly.cpp ----------------------------------- #include "Hourly.h" #include using namespace std; Hourly::Hourly():Employee() { this->payperhr = 0; this->hrsworked = 0; } Hourly::Hourly(int idNum, string lname, string fname):Employee(idNum, lname, fname) { this->payperhr = 0; this->hrsworked = 0; } Hourly::Hourly(int idNum, string lname, string fname, double newpay): Employee(idNum, lname, fname) { this->payperhr = newpay; this->hrsworked = 0; } void Hourly::setPay(double pphr) { this->payperhr = pphr; } void Hourly::setHrs(double nhr) { this->hrsworked = nhr; } double Hourly::getPay() { return payperhr; } double Hourly::getHrs() { return hrsworked; } void Hourly::displayHrly() { cout << endl << "Name " << this->getLName(); cout << endl << "ID Num " << this->getIDNum(); cout << endl << "Pay per hour " << this->payperhr; cout << endl << "------------------------------"; } void Hourly::display() { Employee::display(); cout << endl << "Pay per hour " << this->payperhr; cout << endl << "Hours worked : " << this->hrsworked; cout << endl << "------------------------------"; }
P.S : Updations are done with a comment "///**updation**"
Employee.h
-----------------------------------
#ifndef Employee_H
#define Employee_H
#include
using namespace std;
class Employee
{
private:
string lastName;
string firstName;
int idNum;
public:
Employee(); //default values assigned to all variables
Employee(int idNum, string lname, string fname);
void setLName(string);
void setFName(string);
void setIDNum(int);
string getLName();
string getFName();
int getIDNum();
void displayName();
virtual void display(); ////**updation**
virtual double weeklyEarning()=0;
};
#endif
-----------------------------------
-----------------------------------
Employee.cpp
-----------------------------------
#include
#include
#include"Employee.h"
using namespace std;
Employee::Employee() {
//default values assigned to all variables
lastName = "";
firstName = "";
idNum = 0;
}
Employee::Employee(int idNum, string lname, string fname) {
this->lastName = lname;
this->firstName = fname;
this->idNum = idNum;
}
//Sets last name
void Employee::setLName(string lname) {
this->lastName = lname;
}
//sets first name
void Employee::setFName(string fname) {
this->firstName = fname;
}
//sets id num
void Employee::setIDNum(int idnum) {
this->idNum = idnum;
}
//get last name
string Employee::getLName() {
return lastName;
}
//get first name
string Employee::getFName() {
return firstName;
}
//get id num
int Employee::getIDNum() {
return idNum;
}
//display id and last name
void Employee::displayName() {
cout << endl << "ID Num : " << this->idNum;
cout << endl << "Last name : " << this->lastName;
}
//display idNum,firstName,lastName
void Employee::display() ///**updation**
{
cout << endl << "ID Number " << this->idNum;
cout << endl << "First name : " << this->firstName;
cout << endl << "Last name : " << this->lastName;
}
-----------------------------------
-----------------------------------
Salaried.h
-----------------------------------
#ifndef Salaried_H
#define Salaried_H
#include "Employee.h"
#include
using namespace std;
class Salaried : public Employee{
private:
double weeklySal;
public:
Salaried();
Salaried(int idNum, string lname, string fname);
Salaried(int idNum, string lname, string fname, double sal);
void setSal(double nsal);
double getSal();
void displaySal();
void display();
double weeklyEarning(); ///**updation**
};
#endif
-----------------------------------
-----------------------------------
Salaried.cpp
-----------------------------------
#include "Salaried.h"
#include
using namespace std;
Salaried::Salaried():Employee() {
this->weeklySal = 0;
}
Salaried::Salaried(int idNum, string lname, string fname) :Employee(idNum, lname, fname) {
this->weeklySal = 0;
}
Salaried::Salaried(int idNum, string lname, string fname, double sal) : Employee(idNum, lname, fname) {
this->weeklySal = sal;
}
//set salary
void Salaried::setSal(double nsal) {
this->weeklySal = nsal;
}
//get salary
double Salaried::getSal() {
return weeklySal;
}
//display name, id num, salary
void Salaried::displaySal() {
cout << endl << "Name " << this->getLName();
cout << endl << "ID Num " << this->getIDNum();
cout << endl << "Weekly salary " << this->weeklySal;
cout << endl << "------------------------------";
}
//Display employee details
void Salaried::display() {
Employee::display();
cout << endl << "Weekly salary " << this->weeklySal;
cout << endl << "------------------------------";
}
//returns weekly earnings
double Salaried:: weeklyEarning()
{
return weeklySal; ///**updation**
}
-----------------------------------
-----------------------------------
Commission.h
-----------------------------------
#ifndef Commission_H
#define Commission_H
#include
#include "Employee.h"
using namespace std;
class Commission : public Employee{
private:
double basesalary;
double comAmount;
public:
Commission();
Commission(int idNum, string lname, string fname);
Commission(int idNum, string lname, string fname, double bsal);
void setBase(double npay);
void setCom(double nhr);
double getBase();
double getCom();
void displayBase();
void displayEmp();
double weeklyEarning(); ///**updation**
};
#endif
-----------------------------------
-----------------------------------
Commission.cpp
-----------------------------------
#include "Commission.h"
#include
using namespace std;
//sets Commission instance to default intial values
Commission::Commission():Employee() {
this->basesalary = 0;
this->comAmount = 0;
}
//Sets Commission instance to Commission parameters
Commission::Commission(int idNum, string lname, string fname):Employee(idNum, lname, fname) {
this->basesalary = 0;
this->comAmount = 0;
}
//Sets Commission instance to Employee plus Commision parameters
Commission::Commission(int idNum, string lname, string fname, double bsal):Employee(idNum, lname, fname) {
this->comAmount = 0;
this->basesalary = bsal;
}
//Sets base salary
void Commission::setBase(double npay) {
this->basesalary = npay;
}
//Set commision salary
void Commission::setCom(double nhr) {
this->comAmount = nhr;
}
//Get base salary
double Commission::getBase() {
return basesalary;
}
//Get commission salary
double Commission::getCom() {
return comAmount;
}
//Display base salary and commision salary
void Commission::displayBase() {
cout << endl << "Name " << this->getLName();
cout << endl << "ID Num " << this->getIDNum();
cout << endl << "Base salary " << this->basesalary;
cout << endl << "------------------------------";
}
//Display commission employee details
void Commission::displayEmp() {
Employee::display();
cout << endl << "Base salary " << this->basesalary;
cout << endl << "Commission Amount : " << this->comAmount;
cout << endl << "------------------------------";
}
//returns weekly earnings
double Commission:: weeklyEarning()
{
return basesalary+comAmount; ///**updation**
}
-----------------------------------
-----------------------------------
Hourly.h
-----------------------------------
#ifndef Hourly_H
#define Hourly_H
#include
#include"Employee.h"
using namespace std;
class Hourly : public Employee{
private:
double payperhr;
double hrsworked;
public:
Hourly();
Hourly(int idNum, string lname, string fname);
Hourly(int idNum, string lname, string fname, double newpay);
void setPay(double);
void setHrs(double nhr);
double getPay();
double getHrs();
void displayHrly();
void display();
double weeklyEarning(); ///**updation**
};
#endif
-----------------------------------
-----------------------------------
Hourly.cpp
-----------------------------------
#include "Hourly.h"
#include
using namespace std;
Hourly::Hourly():Employee() {
this->payperhr = 0;
this->hrsworked = 0;
}
Hourly::Hourly(int idNum, string lname, string fname):Employee(idNum, lname, fname) {
this->payperhr = 0;
this->hrsworked = 0;
}
Hourly::Hourly(int idNum, string lname, string fname, double newpay): Employee(idNum, lname, fname) {
this->payperhr = newpay;
this->hrsworked = 0;
}
void Hourly::setPay(double pphr) {
this->payperhr = pphr;
}
void Hourly::setHrs(double nhr) {
this->hrsworked = nhr;
}
double Hourly::getPay() {
return payperhr;
}
double Hourly::getHrs() {
return hrsworked;
}
void Hourly::displayHrly() {
cout << endl << "Name " << this->getLName();
cout << endl << "ID Num " << this->getIDNum();
cout << endl << "Pay per hour " << this->payperhr;
cout << endl << "------------------------------";
}
void Hourly::display() {
Employee::display();
cout << endl << "Pay per hour " << this->payperhr;
cout << endl << "Hours worked : " << this->hrsworked;
cout << endl << "------------------------------";
}
//returns weekly earnings
double Hourly :: weeklyEarning()
{
return (payperhr*hrsworked); ///**updation**
}