Practice using inheritance Practice using polymorphism Practice using an STL con
ID: 3831478 • Letter: P
Question
Practice using inheritance
Practice using polymorphism
Practice using an STL container
Program should be in C++
I have 90% program done. I am not allowed to make changes in the driver file.Also few things are missing Can you please check the code, correct it and also compile it. (run it). I have also added input and output file.The link is also provide so you can match output and also see the actual assignment, given by my professor you can also find the second 2nd input file and assignment details. My code and assignment description, input file 1 and output file is given below.
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:
//project6-main.cpp (This is driver code PLEASE DON"T MAKE ANY CHANGES IN THIS code)
#include
#include
#include
#include // for exit()
#include // fixed, setprecision()
#include "Employee.h"
#include "Cashier.h"
#include "Manager.h"
using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
using std::fixed;
using std::setprecision;
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';
// Process the inputs in inputFilename
void processInputs( string inputFilename ){
// declare an STL container named employees here
// open file
ifstream fileStream;
fileStream.open( inputFilename.c_str() );
// verify the file is opened correctly
if( ! fileStream ){
cerr << "ERROR: Can not open input file " << inputFilename << "!" << endl;
exit(1);
}
cout << "Importing instructions from " << inputFilename << " ..." << endl;
char action = '';
// while there's more patients to process
// read in the action and make sure that we're not at the end of the file
while( fileStream >> action ){
if( DEBUG ){ cout << "action: " << action << endl; }
string name;
switch( action ){
case ADD_EMPLOYEE:
// get the employee's name from the file
fileStream >> name;
cout << "Adding employee " << name << endl;
cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case ADD_CASHIER:
// get the employee's name from the file
fileStream >> name;
float hourlyWage;
fileStream >> hourlyWage;
cout << "Adding cashier " << name << endl;
cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case ADD_MANAGER:
// get the employee's name from the file
fileStream >> name;
int id;
fileStream >> id;
float salary;
fileStream >> salary;
cout << "Adding manager " << name << endl;
cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case REMOVE:
// get the employee's name from the file
fileStream >> name;
cout << "Removing " << name << endl;
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( /*int argc, char* argv[] */){
// 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;
}
MY CODE
Employee.h
====
/*
* Employee.h
#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
#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
#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
#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
#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
#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;
}
INPUT (There re two input files)
D
P
F Sophia
R Sophia
E Sophia
D
P
F Sophia
R Sophia
D
F Sophia
R Sophia
C Jackson 77.91
M Emma 123456789 1234.56
E Aiden
C Olivia 42.87
M Lucas 987654321 7890.12
P
D
R Jackson
R Emma
R Aiden
R Olivia
R Lucas
D
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
Please enter the input filename (or simply press return to use project6-testA.tab)
Importing instructions from project6-testA.tab ...
Displaying 0 employees:
Weekly payroll:
Finding Sophia
Unable to find Sophia
Removing Sophia
Unable to remove Sophia
Adding employee Sophia
Displaying 1 employee:
Sophia
Weekly payroll:
Sophia $290.00
Finding Sophia
Sophia
Removing Sophia
Displaying 0 employees:
Finding Sophia
Unable to find Sophia
Removing Sophia
Unable to remove Sophia
Adding cashier Jackson
Adding manager Emma
Adding employee Aiden
Adding cashier Olivia
Adding manager Lucas
Weekly payroll:
Jackson $3116.40
Emma $1234.56
Aiden $290.00
Olivia $1714.80
Lucas $7890.12
Displaying 5 employees:
Jackson (Cashier hourly wage: $77.91)
Emma (Manager id: 123456789, weekly wage: $1234.56)
Aiden
Olivia (Cashier hourly wage: $42.87)
Lucas (Manager id: 987654321, weekly wage: $7890.12)
Removing Jackson
Removing Emma
Removing Aiden
Removing Olivia
Removing Lucas
Displaying 0 employees:
RUBRIC:
Points Item
---------- --------------------------------------------------------------
_____ / 10 Documentation:
Header comment block at the beginning of each file with:
+ Your full name
+ Date(s) code was written
+ Description
Comments explaining the role of each variable and major section of code
Program solves the assigned problem using methods described in program description:
_____ / 15 + Uses an STL container correctly
_____ / 15 + Inheritance
_____ / 15 + Polymorphism (including a comment identifying the polymorphic call)
_____ / 23 Output
_____ / 0 Program compiles without errors
_____ / 0 Program executes without crashing
Explanation / Answer
Here is the code for the question. Please note: the link clearly says you will NEED TO extend the driver program and you need to handin the updated driver code. So without changing the driver code the solution cannot be completed. Here are the updated files.
Virtual functions can be seen functional when you use pointers and late binding happens. Therefore you need to use new operator to create objects of the classes.
Please note , in the driver file, the following functions have been added - displayAll(), remove() , find(), payroll() . Also the function processInputs() is updated to call these fucntions .
You can compile the files using g++ -std=c++11 Employee.cpp Cashier.cpp Manager.cpp driver.cpp
Sample output for the given input file is attached. Please rate the answer if it helped. Thank you very much.
Employee.h
/*
* Employee.h
*/
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
#include <iomanip>
class Employee
{
protected:
constexpr static double NUM_WORK_HOURS = 40.0;
std::string _name;
float _wage_rate;
public:
Employee(std::string);
Employee(std::string, float);
~Employee();
std::string get_name();
virtual void display();
virtual float get_wage_rate();
virtual float get_weekly_pay();
};
#endif /* EMPLOYEE_H_ */
Employee.cpp
/*
* Employee.cpp
*/
#include "Employee.h"
using namespace std;
Employee::Employee(std::string n)
: _name{ n }, _wage_rate{7.25}
{}
Employee::Employee(std::string n, float w)
: _name{ n }, _wage_rate{ w }
{}
Employee::~Employee()
{}
std::string Employee::get_name()
{
return this->_name;
}
void Employee::display()
{
cout << this->_name;
}
float Employee::get_wage_rate()
{
return _wage_rate;
}
float Employee::get_weekly_pay()
{
return _wage_rate * Employee::NUM_WORK_HOURS;
}
Cashier.h
/* Cashier.h*/
#ifndef CASHIER_H_
#define CASHIER_H_
#include "Employee.h"
class Cashier :
public Employee
{
public:
Cashier(std::string, float);
~Cashier();
void display();
};
#endif /* CASHIER_H_ */
Cashier.cpp
/*
* Cashier.cpp
*/
#include "Cashier.h"
using namespace std;
Cashier::Cashier(string n, float w) :
Employee{n, w}
{}
Cashier::~Cashier()
{}
void Cashier::display()
{
// Will give cashier's name.
Employee::display();
cout << " (Cashier hourly wage: $"<<get_wage_rate()<< ")";
}
Manager.h
/*
* Manager.h
*/
#ifndef MANAGER_H_
#define MANAGER_H_
#include "Employee.h"
class Manager :
public Employee
{
private:
int _manager_id;
public:
Manager(std::string, int, float);
~Manager();
void display();
float get_weekly_pay();
};
#endif /* MANAGER_H_ */
Manager.cpp
/*
* Manager.cpp
*/
#include "Manager.h"
using namespace std;
Manager::Manager(string n, int id, float w) :
Employee{ n,w }, _manager_id(id)
{}
Manager::~Manager()
{}
void Manager::display() {
// Will give manager's name.
Employee::display();
cout << " (Manager id: " << this->_manager_id
<< ", (weekly wage: $" << get_weekly_pay() <<")";
}
//override
float Manager::get_weekly_pay()
{
return _wage_rate;
}
driver.cpp
#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>// for exit()
#include <iomanip>// fixed, setprecision()
#include "Employee.h"
#include "Cashier.h"
#include "Manager.h"
using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
using std::fixed;
using std::setprecision;
using std::vector;
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';
//display all employees
void displayAll( vector<Employee *> &employees)
{
cout<<"Displaying "<<employees.size() << " employees: "<<endl;
Employee *e;
for(vector<Employee *>::iterator it = employees.begin(); it != employees.end(); ++it)
{
cout<< " " ;
e=(*it);
e->display(); //calling the overriden virtual function
cout<<endl;
}
cout<<endl;
}
//display the weeekly payroll
void payroll( vector<Employee *> &employees)
{
cout<<"Weekly payroll:"<<endl;
for( vector<Employee *>::iterator it = employees.begin(); it != employees.end(); ++it)
{
//calling the overriden virtual function get_weekly_pay
cout<< " " << (*it)->get_name() << " $" << (*it)->get_weekly_pay()<<endl;
}
cout<<endl;
}
void remove(vector <Employee *> &employees,string name)
{
for(vector<Employee *>::iterator it = employees.begin(); it != employees.end(); ++it)
{
if((*it)->get_name() == name)
{
delete *it; //delete the allocated object
employees.erase(it); //remove the pointer from vector
cout<<endl;
return;
}
}
cout<<"Unable to remove "<<name<<endl<<endl;
}
void find( vector<Employee *> &employees, string name)
{
for(vector<Employee *>::iterator it = employees.begin(); it != employees.end(); ++it)
{
if((*it)->get_name() == name)
{
cout<<(*it)->get_name()<<endl<<endl;
return;
}
}
cout<<"Unable to find "<<name<<endl<<endl;
}
// Process the inputs in inputFilename
void processInputs( string inputFilename ){
// declare an STL container named employees here
vector<Employee *> employees;
// open file
ifstream fileStream;
fileStream.open( inputFilename.c_str() );
// verify the file is opened correctly
if( ! fileStream ){
cerr << "ERROR: Can not open input file " << inputFilename << "!" << endl;
exit(1);
}
cout << "Importing instructions from " << inputFilename << " ..." << endl;
char action = '';
// while there's more patients to process
// read in the action and make sure that we're not at the end of the file
while( fileStream >> action ){
if( DEBUG ){ cout << "action: " << action << endl; }
string name;
switch( action ){
case ADD_EMPLOYEE:
// get the employee's name from the file
fileStream >> name;
cout << "Adding employee " << name << endl;
cout << endl;
employees.push_back(new Employee(name));
if( DEBUG ){ displayAll( employees); }
break;
case ADD_CASHIER:
// get the employee's name from the file
fileStream >> name;
float hourlyWage;
fileStream >> hourlyWage;
cout << "Adding cashier " << name << endl;
cout << endl;
employees.push_back(new Cashier(name,hourlyWage));
if( DEBUG ){ displayAll( employees); }
break;
case ADD_MANAGER:
// get the employee's name from the file
fileStream >> name;
int id;
fileStream >> id;
float salary;
fileStream >> salary;
cout << "Adding manager " << name << endl;
cout << endl;
employees.push_back(new Manager(name,id,salary));
if( DEBUG ){ displayAll( employees); }
break;
case REMOVE:
// get the employee's name from the file
fileStream >> name;
cout << "Removing " << name << endl;
remove(employees, name);
break;
case PAYROLL:
payroll( employees );
break;
case FIND:
// get the employee's name from the file
fileStream >> name;
cout << "Finding " << name << endl;
find(employees, name);
break;
case DISPLAY_ALL:
displayAll( employees );
break;
default:
cerr << "ERROR: Unknown action " << action << "!" << endl;
exit(1);
}
}
// close the file
fileStream.close();
}
int main( /*int argc, char* argv[] */){
// 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.