In: Computer Science
C++ Requires 9 files
It is common for an organization to have different categories of employees, such that one Employee class can not easily describe all types of Employees.In this project, you will code a basic Employee class, and then a number of classes which derive from the Employee class (inherit), adding Employee specifics. An application program which stores info for one company will have use objects of different types to model the all employees.
Part I: The Employee Class
First create the Employee class described below. This class will serve as a base class. All specific employee typed will be derived from this class. :
Employee |
-lastName:string -firstName:string -idNum:int |
+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() +sgtringgetFName() +intgetIDNum() +void displayName() +void display() |
Stick with the access specifiers on the diagram. Items denoted as + are public, - are private.
NOTE: There are NO protected member variables in this class
The constructors both initialize all member variables. Sets functions set the indicated member variable of the object, and get functions return the value of the indicated member variable of the object.
The displayName() function prints out the last name and ID only, and the display()function outputs the all employee information that is stored.
PART II: The Derived Classes
Salaried |
Hourly |
Commission |
||
-weeklySal:double |
-payperhr:double -hrsworked:double |
-basesalary:double -comAmount:double |
||
+Salaried(string lname, string fname) +Salaried(string lname, string fname, double sal) +void setSal(double nsal) +double getSal() +void displaySal() +void display() |
+Hourly(string lname, string fname) +Hourly(string lname, string fname, double newpay) +void setPay(double) +void setHrs(double nhr) +double getPay() +double getHrs() +void displayHrly(): +void display() |
+Commission(string lname, string fname) +Commission(string lname, string fname, double bsal) +void setBase(double npay) +void setCom(double nhr) +double getBase() +double getCom() +void displayBase() +void displayEmp() |
As you can see, Employee is a base class, which is extended by the Hourly, Salaried and Commissioned classes. The derived classes describe 3 ‘types’ of employee.
Each of the derived classes provides instance data necessary for determining an employee’s weekly earnings, and both set (mutator) and get(accessor) methods for these additional variables. A salaried employee just gets a prearranged salary per week. An hourly employee’s weekly salary is based on the hourly rate and number of hours worked that week. A commissioned employee gets a base pay per week, plus the amount of commission earned that week. Each of these classes provides two display functions, one that outputs info specific for that class, one that overrides the base class function display(), to output all info on the object (both base and derived class data).
The displaySal() function outputs the employee name, id and weekly salary.
The displayHrly() function outputs the employee name, id and hourly rate.
The displayBase() function outputs the employee name, id and base pay.
Once these classes are working and tested you are ready to begin part III.
Part III The Application
You are to write an application which will allow the user to enter all his/her employees and their data. This application will allow the user to print out information on employees.
(This project will be updated in a future project assignment, so be sure to stick to the specifications. This will make the update happen more smoothly)
This application is to store each of the three types of objects in it’s own vector. That is, you will have a vector<Salaried>, a vector<Hourly> and a vector<Commission>.
Your program is to provide the following options for the user:
* add a new salaried employee
* add a new hourly employee
* add a new commissioned employee
* list all employees with a particular last name
* output all information for all employees stored
* search for an employee with a particular id number
* output the name and salaries ONLY of all salaried employees
* output the name and hourly rate ONLY of all salaried employees
* output the name, id and base salary of all commissioned employees
You are to submit 9 files, Employee.h, Employee.cpp, Salaried.h, Salaried.cpp, Hourly.h, Hourly.cpp, Commission.h, Commission.cpp, and your main program file.
Following c++ program deomstrates usage of inheritance.
We have one parent class : Employee
Three child classes derives from Employee are : Salaried, Hourly,
Commission.
Each has their own extra features plus employee features.
Implementation of application walk you through the menu driven flow, which will allow you to perform following actions :
1. add a new salaried employee
2. add a new hourly employee
3. add a new commissioned employee
4. list all employees with a particular last name
5. output all information for all employees stored
6. search for an employee with a particular id number
7. output the name and salaries ONLY of all salaried
employees
8. output the name and hourly rate ONLY of all hourly
employees
9. output the name, id and base salary of all commissioned
employees
0. Exit
There are total 9 code files :
Employee.h, Employee.cpp, Salaried.h, Salaried.cpp, Commission.h,
Commission.cpp, Hourly.h, Hourly.cpp, main.cpp.
and
output image file.
Source code :
-----------------------------------
Employee.h
-----------------------------------
#ifndef Employee_H
#define Employee_H
#include<string>
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<string>
#include<iostream>
#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<string>
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<iostream>
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<string>
#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 <iostream>
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<string>
#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 <iostream>
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 <<
"------------------------------";
}
-----------------------------------
-----------------------------------
main.cpp
-----------------------------------
#include<iostream>
#include<vector>
#include "Salaried.h";
#include "Hourly.h";
#include "Commission.h";
using namespace std;
//Menu for all features
void menu();
//driver method
int main() {
//store all salariedEmployees
vector<Salaried> salariedEmployees;
//store all hourlyEmployees
vector<Hourly> hourlyEmployees;
//store all commissionEmployees
vector<Commission> commissionEmployees;
//to store menu option
int choice;
//local variables for reading info
double weeklySal;
string lastName;
string firstName;
int idNum;
double payperhr;
double hrsworked;
double basesalary;
double comAmount;
//Search employee id
int searchIDNum;
Salaried salariedEmp = Salaried();
Hourly hourlyEmp = Hourly();
Commission commissionEmp = Commission();
do {
//Dsiplay menu
menu();
//Ask user for menu option
cout << endl << "Enter
your choice :";
//Read menu option
cin >> choice;
//Based on selected menu option,
specific case will be executed
switch (choice) {
case 1:
//add a new
salaried employee
cout <<
endl << "Enter salaried employee details :" <<
endl;
cout <<
endl << "First name :"; cin >> firstName;
cout <<
endl << "Last name :"; cin >> lastName;
cout <<
endl << "ID Num :"; cin >> idNum;
cout <<
endl << "Weekly Salary :"; cin >> weeklySal;
salariedEmp.setFName(firstName);
salariedEmp.setLName(lastName);
salariedEmp.setIDNum(idNum);
salariedEmp.setSal(weeklySal);
salariedEmployees.push_back(salariedEmp);
cout <<
endl << "salaried employee details added." << endl;
break;
case 2:
//add a new
hourly employee
cout <<
endl << "Enter Hourly employee details :" << endl;
cout <<
endl << "First name :"; cin >> firstName;
cout <<
endl << "Last name :"; cin >> lastName;
cout <<
endl << "ID Num :"; cin >> idNum;
cout <<
endl << "Pay per Hour :"; cin >> payperhr;
cout <<
endl << "Hours worked :"; cin >> hrsworked;
hourlyEmp.setFName(firstName);
hourlyEmp.setLName(lastName);
hourlyEmp.setIDNum(idNum);
hourlyEmp.setPay(payperhr);
hourlyEmp.setHrs(hrsworked);
hourlyEmployees.push_back(hourlyEmp);
cout <<
endl << "Hourly employee details added." << endl;
break;
case 3:
//add a new
commissioned employee
cout << endl << "Enter Commission employee details :" << endl;
cout <<
endl << "First name :"; cin >> firstName;
cout <<
endl << "Last name :"; cin >> lastName;
cout <<
endl << "ID Num :"; cin >> idNum;
cout <<
endl << "Base salary :"; cin >> basesalary;
cout <<
endl << "Commission amount :"; cin >> comAmount;
commissionEmp.setFName(firstName);
commissionEmp.setLName(lastName);
commissionEmp.setIDNum(idNum);
commissionEmp.setBase(basesalary);
commissionEmp.setCom(comAmount);
commissionEmployees.push_back(commissionEmp);
cout <<
endl << "Commission employee details added." <<
endl;
break;
case 4:
//list all
employees with a particular last name
cout <<
endl << "Enter the last name (employees with mentioned last
name's details will be displayed) : ";
cin >>
lastName;
cout << endl << "Displaying employee who's has last name :" << lastName << endl;
for (auto& it : hourlyEmployees) {
if(it.getLName() == lastName)
it.display();
}
for
(auto& it : salariedEmployees) {
if (it.getLName() == lastName)
it.display();
}
for
(auto& it : commissionEmployees) {
if (it.getLName() == lastName)
it.display();
}
cout <<
endl << "Done displaying employee names";
break;
case 5:
//output all
information for all employees stored
cout << endl << "Displaying all employee details "<< endl;
for (auto& it : hourlyEmployees) {
it.display();
}
for
(auto& it : salariedEmployees) {
it.display();
}
for
(auto& it : commissionEmployees) {
it.display();
}
cout <<
endl << "Done displaying all employee details";
break;
case 6:
//search for an
employee with a particular id number
cout <<
endl << "Enter the search IDNum : ";
cin >>
searchIDNum;
cout << endl << "Displaying employee who's has ID Num :" << searchIDNum << endl;
for (auto& it : hourlyEmployees) {
if (it.getIDNum() == searchIDNum)
it.display();
}
for
(auto& it : salariedEmployees) {
if (it.getIDNum() == searchIDNum)
it.display();
}
for
(auto& it : commissionEmployees) {
if (it.getIDNum() == searchIDNum)
it.display();
}
cout <<
endl << "Done displaying employee details with mentioned ID
num";
break;
case 7:
//output the
name and salaries ONLY of all salaried employees
cout << endl << "Displaying name and salary of all salaried employees" << endl;
for (auto&
it : salariedEmployees) {
cout << endl << "First Name : "
<< it.getFName();
cout << endl << "Last Name : "
<< it.getLName();
cout << endl << "Salary : " <<
it.getSal();
}
cout << endl << "Done displaying all salaried employee details";
break;
case 8:
//output the
name and hourly rate ONLY of all hourly employees
cout << endl << "Displaying name and hourly rates of all hourly employees" << endl;
for (auto&
it : hourlyEmployees) {
cout << endl << "First Name : "
<< it.getFName();
cout << endl << "Last Name : "
<< it.getLName();
cout << endl << "Hourly Rate : "
<< it.getPay();
}
cout << endl << "Done displaying all hourly employee details";
break;
case 9:
//output the
name, id and base salary of all commissioned employees
cout <<
endl << "Displaying name , id and base salary of all
commissioned employees" << endl;
for (auto&
it : commissionEmployees) {
cout << endl << "First Name : "
<< it.getFName();
cout << endl << "Last Name : "
<< it.getLName();
cout << endl << "ID Num : " <<
it.getIDNum();
cout << endl << "Base Salary : "
<< it.getBase();
}
cout << endl << "Done displaying all commission employee details";
break;
case 0:
//exit
cout <<
endl << "Exiting!";
return 0;
break;
default:
cout <<
endl << "Invalid choice, try again!";
}
} while (1);
return 0;
}
void menu() {
cout << endl << "---Employee database---"
<< endl;
cout << endl << "1. add a new salaried
employee";
cout << endl << "2. add a new hourly
employee";
cout << endl << "3. add a new commissioned
employee";
cout << endl << "4. list all employees
with a particular last name";
cout << endl << "5. output all information
for all employees stored";
cout << endl << "6. search for an employee
with a particular id number";
cout << endl << "7. output the name and
salaries ONLY of all salaried employees";
cout << endl << "8. output the name and
hourly rate ONLY of all hourly employees";
cout << endl << "9. output the name, id
and base salary of all commissioned employees";
cout << endl << "0. Exit";
}
-----------------------------------
-----------------------------------
output
-----------------------------------
-----------------------------------
Let me know , if you face any issue.