Practice using inheritance Practice using polymorphism Practice using an STL con
ID: 3830772 • Letter: P
Question
Practice using inheritance
Practice using polymorphism
Practice using an STL container
Program should be in C++
I currently have the .h files for each employee.h cashier.h and manager.h I need help making their .cpp. I want to take what I have in the .h file and make it into .cpp. I have made a .cpp file for header file employee.h but I am not confident about it. Please take a look if you need more information then just let me know.
For this project, you will write a complete C++ program to handle a staff directory and payroll. Extend the driver code located in project6-main.cpp. You need to declare an STL container named employees in the processInputs() function. Additionally, you need to write the Employee, Cashier and Manager classes. Also, you need to use inheritance and polymorphism appropriately. The Employee class needs to store the employee's name. Employees are paid minimum wage ($7.25) for 40 hours each week. The Cashier class needs to store the cashier name and their hourly wage. Cashiers are paid this hourly wage for 40 hours each week. The Manager class needs to store the manager's name, their ID and their weekly salary.
Additionally, add a comment identifying the polymorphic method call in project6-main.cpp.
You can use the following input files for this project:
DRIVER CODE // project6-main.cpp
My Work
File: Employee.h
#ifndef Employee_H
#define Employee_H
#include
using namespace std;
class Employee
{
private:
constexpr static const float NUM_WORK_HOURS = 40.0;
constexpr static const float MIN_WAGE_PER_HOUR = 7.25; // $7.25
protected:
string name;
float wagePerHour;
public:
Employee(string aName);
Employee(string aName, float aWagePerHour);
string getName();
virtual void display();
virtual void payroll();
};
#endif
File Employee.cpp
#include "Employee.h"
#include
using namespace std;
Employee::Employee(string aName) { name = aName; wagePerHour = MIN_WAGE_PER_HOUR;}
Employee::Employee(string aName, float aWagePerHour) {
if (aWagePerHour < MIN_WAGE_PER_HOUR) {
aWagePerHour = MIN_WAGE_PER_HOUR;
}
wagePerHour = aWagePerHour;
name = aName;
}
string Employee::getName() { return name; }
void Employee::display() { cout << name << endl; }
void Employee::payroll() {
cout << name << " $" << fixed << setprecision(2) << (wagePerHour * NUM_WORK_HOURS) << endl;
}
File: Cashier.h
#ifndef Included_Cashier_H
#define Included_Cashier_H
#include
#include "Employee.h"
using namespace std;
class Cashier : public Employee
{
public:
Cashier(string aName, float aWagePerHour) : Employee(aName, aWagePerHour) { }
virtual void display() {
cout << name << " (Cashier hourly wage: $" << fixed << setprecision(2) << wagePerHour << ")" << endl;
}
};
#endif // Included_Cashier_H
File: Manager.h
#ifndef Included_Manager_H
#define Included_Manager_H
#include
#include "Employee.h"
using namespace std;
class Manager : public Employee
{
private:
int managerId;
float wagePerWeek;
public:
Manager(string aName, int aManagerId, float aWagePerWeek) : Employee(aName), managerId(aManagerId) {
wagePerWeek = aWagePerWeek;
}
virtual void display() {
cout << name << " (Manager id: " << managerId << ", weekly wage: $" << fixed << setprecision(2) << wagePerWeek << ")" << endl;
}
virtual void payroll() {
cout << name << " $" << fixed << setprecision(2) << wagePerWeek << endl;
}
};
#endif // Included_Manager_H
INPUT (There re two input files)
input file 2:
other input file is large so i am attaching link to access that file (in Link assignment detail is also present)
https://drive.google.com/open?id=0B8OHmjK8INMndVV3QlBWdk5KeFE
OUTPUT
RUBRIC:
Explanation / Answer
All your code looks okay, I'll repost it anyways.
Employee.h
====
/*
* Employee.h
*
* Created on: 02-May-2017
* Author: Rj
*/
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
#include <iomanip>
class Employee
{
private:
constexpr static double NUM_WORK_HOURS = 40.0;
constexpr static double MIN_WAGE_PER_HOUR = 7.25;
protected:
std::string _name;
double _wage_per_hour;
public:
Employee(std::string);
Employee(std::string, double);
virtual ~Employee();
std::string get_name();
virtual void display();
virtual void payroll();
};
#endif /* EMPLOYEE_H_ */
Employee.cpp
====
/*
* Employee.cpp
*
* Created on: 02-May-2017
* Author: Rj
*/
#include "Employee.h"
using namespace std;
Employee::Employee(std::string n)
: _name{ n }, _wage_per_hour{}
{}
Employee::Employee(std::string n, double w)
: _name{ n }, _wage_per_hour{ w }
{}
Employee::~Employee()
{}
std::string Employee::get_name()
{
return this->_name;
}
void Employee::display()
{
cout << this->_name;
}
void Employee::payroll()
{
cout << " $"
<< fixed << setprecision(2)
<< (this->_wage_per_hour * this->NUM_WORK_HOURS);
}
Cashier.h
====
/*
* Cashier.h
*
* Created on: 02-May-2017
* Author: Rj
*/
#ifndef CASHIER_H_
#define CASHIER_H_
#include "Employee.h"
class Cashier :
public Employee
{
public:
Cashier(std::string, double);
virtual ~Cashier();
virtual void display();
};
#endif /* CASHIER_H_ */
Cashier.cpp
====
/*
* Cashier.cpp
*
* Created on: 02-May-2017
* Author: Rj
*/
#include "Cashier.h"
using namespace std;
Cashier::Cashier(string n, double w) :
Employee{n, w}
{}
Cashier::~Cashier()
{}
void Cashier::display()
{
// Will give cashier's name.
Employee::display();
cout << " (Cashier hourly wage: ";
Employee::payroll();
cout << ")" << endl;
}
Manager.h
====
/*
* Manager.h
*
* Created on: 02-May-2017
* Author: Rj
*/
#ifndef MANAGER_H_
#define MANAGER_H_
#include "Employee.h"
class Manager :
public Employee
{
private:
int _manager_id;
double _wage_per_week;
public:
Manager(std::string, int, double);
virtual ~Manager();
virtual void display();
};
#endif /* MANAGER_H_ */
Manager.cpp
====
/*
* Manager.cpp
*
* Created on: 02-May-2017
* Author: Rj
*/
#include "Manager.h"
using namespace std;
Manager::Manager(string n, int id, double w) :
Employee{ n }, _manager_id(id), _wage_per_week{ w }
{}
Manager::~Manager()
{}
void Manager::display() {
// Will give cashier's name.
Employee::display();
cout << " (Manager id: " << this->_manager_id
<< " (Cashier hourly wage: ";
Employee::payroll();
cout << ")" << endl;
}
//============================================================================
// Name : main.cpp
// Author : Ramachandra jr
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "Employee.h"
#include "Cashier.h"
#include "Manager.h"
using namespace std;
bool DEBUG = false; // toggles extra printing
const string DEFAULT_INPUT_FILENAME = "project6-testA.tab";
// Action characters in the input file
const char ADD_EMPLOYEE = 'E';
const char ADD_CASHIER = 'C';
const char ADD_MANAGER = 'M';
const char REMOVE = 'R';
const char PAYROLL = 'P';
const char FIND = 'F';
const char DISPLAY_ALL = 'D';
/**
* Displays all employees.
*/
void displayAll(vector<Employee> e)
{
vector<Employee>::iterator it;
for (it = e.begin(); it < e.end(); ++it)
{
*it->display();
}
}
while(fileStream >> action)
{
if(DEBUG)
cout << "action: " << action << endl;
string name;
switch(action)
{
case ADD_EMPLOYEE:
fileStream >> name;
cout << "Adding employee " << name << endl;
cout << endl;
// Adding an employee.
emp0 = new Employee{ name };
employees.push_back(*emp0);
if(DEBUG)
displayAll(employees);
break;
case ADD_CASHIER:
fileStream >> name;
double hourlyWage;
fileStream >> hourlyWage;
cout << "Adding cashier " << name << endl;
cout << endl;
// Adding a cashier.
emp0 = new Cashier{name, hourlyWage};
employees.push_back(*emp0);
if( DEBUG )
displayAll(employees);
break;
case ADD_MANAGER:
fileStream >> name;
int id;
fileStream >> id;
float salary;
fileStream >> salary;
cout << "Adding manager " << name << endl;
cout << endl;
// Adding manager
emp0 = new Manager{name, id, salary};
employees.push_back(*emp0);
if( DEBUG )
displayAll(employees);
break;
case REMOVE:
// get the employee's name from the file
fileStream >> name;
cout << "Removing " << name << endl;
// Removing an employee.
for (it = employees.begin(); it < employees.end(); ++it)
{
*it->display();
}
break;
case PAYROLL:
payroll(employees);
break;
case FIND:
// get the employee's name from the file
fileStream >> name;
cout << "Finding " << name << endl;
break;
case DISPLAY_ALL:
displayAll(employees );
break;
default:
cerr << "ERROR: Unknown action " << action << "!" << endl;
exit(1);
}
}
// close the file
fileStream.close();
}
int main(){
// If just a return (' ') is entered, then use DEFAULT_INPUT_FILENAME.
// Otherwise, read in the input filename from STDIN.
string inputFilename;
cout << "Please enter the input filename (or simply press return to use "
<< DEFAULT_INPUT_FILENAME << ") ";
getline(cin, inputFilename);
if( inputFilename == ""){
inputFilename = DEFAULT_INPUT_FILENAME;
}
// process transactions in the file
processInputs( inputFilename );
return 0;
}
The questionwas quite too long, I had to study all the .h and .cpp files to make sure everything is okay. Conventions were a bit off I corrected most of them. What's left is to implement two other functions a find and a remove. Both of them are not at all hard. have a look at them in the internet. For STL container I used a vector because it gets the job done quite well here. Try to implement find, it is similar to display as written above except you do not call display(), you just have to see if (*it->get_name == name). Sorry for not being able to answer it completely.
Cheers,
Rj
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.