Ok, I wanted to try and avoid posting this whole solution, but I don't see any other to address the problems I'm having. So I apologize in advance, this is going to be a long post.
Same solution that I was having compiler errors on yesterday. We're working with inheritance and the program is supposed to be to create and display information for default Employee objects, Hourly Employee objects and Salaried Employee objects. Everything compiles as is, but I'm running into two issues that I can't figure out.
1) In both the hourly and salaried objects, the wage, hours, and management level attributes all displaying their default values. I'm almost positive that this has something to do with the str to int and str to double conversions that I'm using (currently have atoi and atof in the file, but I've also tried stringstream, but still had to same problem). Any thoughts as to what I'm missing?
2) the assignment calls for the Benefit benefit member in Employee.h to be protected. However this makes the member unaccessible when I try to use it in the EmployeeMain.cpp in order to set the input for the Benefit class members. I had to make the Benefit benefit member public again to get it to compile. How would I set the input for the Benefit class members while the Benefit benefit member is protected?
Thanks in advance for any help.
Solution files follow:
EmployeeMain.cpp
Code:
#include "Hourly.h"
#include "Salaried.h"
void DisplayApplicationInformation();
void DisplayDivider(string);
string GetInput(string);
void TerminateApplication();
int Employee::numEmployees = 0;
int main()
{
DisplayApplicationInformation();
string input = "";
DisplayDivider(" EMPLOYEE #1 ");
cout << endl;
Benefit benefit1("Atena", 250000, 45);
Employee emp1("John","Jones",'M', 3, 100000, benefit1);
emp1.Employee::displayEmployee();
DisplayDivider("Number of Employees");
cout << "Number of employees: " << Employee::getnumEmployees() << endl;
cout << endl;
DisplayDivider(" EMPLOYEE #2 ");
cout << endl;
Salaried emp2;
input = GetInput("First Name");
emp2.setFirstName(input);
input = GetInput("Last Name");
emp2.setLastName(input);
input = GetInput("Gender");
emp2.setGender(input.at(0)); //.at() converts str to char
input = GetInput("Dependent");
emp2.setDependent(input); //overloaded method, str to int.
input = GetInput("Annual Salary");
emp2.setAnnualSalary(input); //overloaded method, str to double
input = GetInput("Health Insurance Company");
emp2.benefit.setHealthInsurance(input);
input = GetInput("Amount of Life Insurance");
emp2.benefit.setLifeInsurance(atof(input.c_str()));
input = GetInput("Vacation Time");
emp2.benefit.setVacation(atoi(input.c_str()));
input = GetInput("Management Level, 0-3");
emp2.setManagementLevel(atoi(input.c_str()));
cout << endl;
emp2.Salaried::displayEmployee();
DisplayDivider("Number of Employees");
cout << "Number of employees: " << Employee::getnumEmployees() << endl;
cout << endl;
DisplayDivider(" EMPLOYEE #3 ");
cout << endl;
Hourly emp3;
input = GetInput("First Name");
emp3.setFirstName(input);
input = GetInput("Last Name");
emp3.setLastName(input);
input = GetInput("Gender");
emp3.setGender(input.at(0));
input = GetInput("Dependent");
emp3.setDependent(input);
input = GetInput("Employment Status? - FT, PT, TEMP");
emp3.setCatagory(input);
input = GetInput("Hourly Wage");
emp3.setWage(atof(input.c_str()));
input = GetInput("Number of Hours Worked");
emp3.setHours(atof(input.c_str()));
input = GetInput("Health Insurance Company");
emp3.benefit.setHealthInsurance(input);
input = GetInput("Amount of Life Insurance");
emp3.benefit.setLifeInsurance(atof(input.c_str()));
input = GetInput("Vacation Time");
emp3.benefit.setVacation(atoi(input.c_str()));
cout << endl;
emp3.Hourly::displayEmployee();
DisplayDivider("Number of Employees");
cout << "Number of employees: " << Employee::getnumEmployees() << endl;
cout << endl;
TerminateApplication();
system("pause");
return 0;
}
void DisplayApplicationInformation()
{
cout << endl;
cout << "Welcome to the Employee Class Program" << endl;
cout << "CIS247C, Week5 Lab" << endl;
cout << "Developer: John Worley" << endl;
cout << endl;
}
void DisplayDivider(string outputTitle)
{
cout << setw(12) << outputTitle << setw(12) << endl;
cout << "-----------------------" << endl;
}
string GetInput(string input) //general input function; takes input as string
{
cout << "Enter " << input << ": "; // no endl, in order to allow input on same line
getline(cin, input);
return input;
}
void TerminateApplication() // terminate program message
{
cout << "Thank you for using the Employee class program" << endl;
cout << "Program terminating" << endl;
cout << "Good-Bye" << endl;
cout << endl;
}
Benefit.h
Code:
# pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Benefit
{
private:
string HealthInsurance;
double LifeInsurance;
int Vacation;
public:
Benefit();
Benefit(string health, double life, int vac);
void displayBenefits();
string getHealthInsurance();
void setHealthInsurance(string health);
double getLifeInsurance();
void setLifeInsurance(double life);
int getVacation();
void setVacation(int vac);
};
Benefit.cpp
Code:
#include "Benefit.h"
static const double Min_Life = 0;
static const double Max_Life = 1000000;
static const int Min_Vacation = 0;
static const int Max_Vacation = 365;
Benefit::Benefit()
{
string HealthInsurance = "NG"; //NG stands for not given
double LifeInsurance = 0;
int Vacation = 0;
}
Benefit::Benefit(string health, double life, int vac)
{
HealthInsurance = health;
LifeInsurance = life;
Vacation = vac;
}
void Benefit::displayBenefits()
{
cout << endl;
cout << "Benefits Information" << endl;
cout << "----------------------------------" << endl;
cout << "Health Insurance Company: " << setw(5) << HealthInsurance << endl;
cout << "Amount of Life Insurance: $" << setw(5) << LifeInsurance << endl;
cout << "Vacation Time: " << setw(13) << Vacation << " days" << endl;
cout << endl;
}
string Benefit::getHealthInsurance()
{
return HealthInsurance;
}
void Benefit::setHealthInsurance(string health)
{
HealthInsurance = health;
}
double Benefit::getLifeInsurance()
{
return LifeInsurance;
}
void Benefit::setLifeInsurance(double life)
{
if(life >= Min_Life && life <= Max_Life)
{
LifeInsurance = life;
}
else if(life < Min_Life)
{
LifeInsurance = Min_Life;
}
else
{
LifeInsurance = Max_Life;
}
}
int Benefit::getVacation()
{
return Vacation;
}
void Benefit::setVacation(int vac)
{
if(vac >= Min_Vacation && vac <= Max_Vacation)
{
Vacation = vac;
}
else if(vac < Min_Vacation)
{
Vacation = Min_Vacation;
}
else
{
Vacation = Max_Vacation;
}
}
Employee.h
Code:
#pragma once
#include "Benefit.h"
class Employee
{
protected:
string FirstName, LastName;
char Gender;
int Dependent;
double AnnualSalary;
private:
static int numEmployees;
public:
Benefit benefit;
Employee();
Employee(string fName, string lName, char gen, int depend, double salary, Benefit ben);
double calculatePay();
void displayEmployee();
string getFirstName();
void setFirstName(string fName);
string getLastName();
void setLastName(string lName);
char getGender();
void setGender(char gen);
int getDependent();
void setDependent(int depend);
void setDependent(string depend);
double getAnnualSalary();
void setAnnualSalary(double salary);
void setAnnualSalary(string salary);
static int getnumEmployees();
Benefit getBenefit();
void setBenefit(Benefit ben);
};
Employee.cpp
Code:
#include "Employee.h"
static const int Max_Depend = 10;
static const int Min_Depend = 0;
static const double Max_Salary = 250000;
static const double Min_Salary = 20000;
static const char Default_Gender = 'U';
Employee::Employee()
{
FirstName = "not given";
LastName = "not given";
Gender = 'U';
Dependent = 0;
AnnualSalary = 20000;
benefit = Benefit();
numEmployees++;
}
Employee::Employee(string fName, string lName, char gen, int depend, double salary, Benefit ben)
{
FirstName = fName;
LastName = lName;
Gender = gen;
Dependent = depend;
AnnualSalary = salary;
benefit = ben;
numEmployees++;
}
double Employee::calculatePay()
{
return AnnualSalary/52;
}
void Employee::displayEmployee()
{
cout << endl;
cout << "Employee Information" << endl;
cout << "----------------------------------" << endl;
cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
cout << "Gender: " << setw(12) << Gender << endl;
cout << "Dependents: " << setw(8) << Dependent << endl;
cout << showpoint << fixed << setprecision(2);
cout << "Annual Salary: " << setw(5) << "$" << AnnualSalary << endl;
cout << "Weekly Salary: " << setw(5) << "$" << calculatePay() << endl;
benefit.displayBenefits();
cout << endl;
}
string Employee::getFirstName()
{
return FirstName;
}
void Employee::setFirstName(string fName)
{
FirstName = fName;
}
string Employee::getLastName()
{
return LastName;
}
void Employee::setLastName(string lName)
{
LastName = lName;
}
char Employee::getGender()
{
return Gender;
}
void Employee::setGender(char gen)
{
switch(gen)
{
case 'f':case 'F': case 'm': case 'M':
Gender = gen;
break;
default:
Gender = Default_Gender;
}
}
int Employee::getDependent()
{
return Dependent;
}
void Employee::setDependent(int depend)
{
if(depend >= Min_Depend && depend <= Max_Depend)
{
Dependent = depend;
}
else if(depend < Min_Depend)
{
Dependent = Min_Depend;
}
else
{
Dependent = Max_Depend;
}
}
void Employee::setDependent(string input) //overloaded method set as string
{
Dependent = atoi(input.c_str()); //str to int
}
double Employee::getAnnualSalary()
{
return AnnualSalary;
}
void Employee::setAnnualSalary(double salary)
{
if(salary >= Min_Salary && salary <= Max_Salary)
{
AnnualSalary = salary;
}
else if(salary < Min_Salary)
{
AnnualSalary = Min_Salary;
}
else
{
AnnualSalary = Max_Salary;
}
}
void Employee::setAnnualSalary(string input)
{
AnnualSalary = atof(input.c_str()); //str to double
}
int Employee::getnumEmployees()
{
return numEmployees;
}
Benefit Employee::getBenefit()
{
return benefit;
}
void Employee::setBenefit(Benefit ben)
{
benefit = ben;
}
Salaried.h
Code:
#include "Employee.h"
class Salaried:public Employee
{
private:
int ManagementLevel;
public:
Salaried();
Salaried(string fName, string lName, char gen, int depend, double salary, Benefit ben, int manLevel);
Salaried(double salary, int manLevel);
int getManagementLevel();
void setManagementLevel(int manLevel);
double CalculatePay();
void displayEmployee();
};
Salaried.cpp
Code:
#include "Salaried.h"
static const int Min_Management = 0;
static const int Max_Management = 3;
static const double Bonus_Percent = 10;
Salaried::Salaried():Employee()
{
int manLevel = 0;
}
Salaried::Salaried(string fName, string lName, char gen, int depend, double salary, Benefit ben, int manLevel):Employee(fName,lName,gen,depend,salary,ben)
{
}
Salaried::Salaried(double salary, int manLevel):Employee()
{
setAnnualSalary(salary);
}
double Salaried::CalculatePay()
{
double Bonus_Percent = ManagementLevel * 0.10;
return (AnnualSalary + Bonus_Percent) / 52;
}
void Salaried::displayEmployee()
{
cout << endl;
cout << "Employee Information" << endl;
cout << "----------------------------------" << endl;
cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
cout << "Gender: " << setw(12) << Gender << endl;
cout << "Dependents: " << setw(8) << Dependent << endl;
cout << "Salaried Employee" << endl;
cout << "Management Level: " << setw(2) << Salaried::getManagementLevel() << endl;
cout << showpoint << fixed << setprecision(2);
cout << "Annual Salary: " << setw(5) << "$" << AnnualSalary << endl;
cout << "Weekly Salary: " << setw(5) << "$" << Salaried::CalculatePay() << endl;
cout << endl;
benefit.displayBenefits();
cout << endl;
}
int Salaried::getManagementLevel()
{
return ManagementLevel;
}
void Salaried::setManagementLevel(int manLevel)
{
if(ManagementLevel >= Min_Management && ManagementLevel <= Max_Management)
{
ManagementLevel = manLevel;
}
else
if(ManagementLevel < Min_Management)
{
ManagementLevel = Min_Management;
}
else
{
ManagementLevel = Max_Management;
}
}
Hourly.h
Code:
#include "Employee.h"
class Hourly:public Employee
{
private:
double Wage;
double Hours;
string Catagory;
public:
Hourly();
Hourly(double hwage, double hrs, string catagory);
Hourly(string fname, string lname, char gen, int dep, double hwage, double hrs, Benefit ben, string catagory);
double CalculatePay();
void displayEmployee();
double getWage();
void setWage(double hwage);
double getHours();
void setHours(double hrs);
string getCatagory();
void setCatagory(string catagory);
double getAnnualSalary();
void setAnnualSalary(double salary);
};
Hourly.cpp
Code:
#include "Hourly.h"
const double MIN_WAGE = 10;
const double MAX_WAGE = 75;
const double MIN_HOURS = 0;
const double MAX_HOURS = 50;
Hourly::Hourly():Employee()
{
double Wage = 0;
double Hours = 0;
string Catagory = "";
}
Hourly::Hourly(double hwage, double hrs, string catagory):Employee()
{
}
Hourly::Hourly(string fName, string lName, char gen, int dep, double hwage, double hrs, Benefit ben, string catagory):Employee()
{
Wage = hwage;
Hours = hrs;
Catagory = catagory;
}
double Hourly::CalculatePay()
{
return Wage * Hours;
}
void Hourly::displayEmployee()
{
cout << endl;
cout << "Employee Information" << endl;
cout << "----------------------------------" << endl;
cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
cout << "Gender: " << setw(12) << Gender << endl;
cout << "Dependents: " << setw(8) << Dependent << endl;
cout << showpoint << fixed << setprecision(2);
cout << "Hourly Employee" << endl;
cout << "Employment Status: " << Hourly::getCatagory() << endl;
cout << "Wage: " << setw(14) << "$" << Wage << " per hour " << endl;
cout << "Hours: " << setw(16) << Hours << endl;
cout << "Annual Salary: " << setw(5) << "$" << Hourly::getAnnualSalary() << endl;
cout << "Weekly Salary: " << setw(5) << "$" << Hourly::CalculatePay() << endl;
cout << endl;
cout << "Benefits Information" << endl;
cout << "----------------------------------" << endl;
cout << "Health Insurance Company: " << setw(5) << benefit.getHealthInsurance() << endl;
cout << "Amount of Life Insurance: $" << setw(5) << benefit.getLifeInsurance() << endl;
cout << "Vacation Time: " << setw(13) << benefit.getVacation() << " days" << endl;
cout << endl;
cout << endl;
}
double Hourly::getWage()
{
return Wage;
}
void Hourly::setWage(double hwage)
{
if(Wage >= MIN_WAGE && Wage <= MAX_WAGE)
{
Wage = hwage;
}
else
if(Wage < MIN_WAGE)
{
Wage = MIN_WAGE;
}
else
{
Wage = MAX_WAGE;
}
}
double Hourly::getHours()
{
return Hours;
}
void Hourly::setHours(double hrs)
{
if(Hours >= MIN_HOURS && Hours <= MAX_HOURS)
{
Hours = hrs;
}
else
if(Hours < MIN_HOURS)
{
Hours = MIN_HOURS;
}
else
{
Hours = MAX_HOURS;
}
}
string Hourly::getCatagory()
{
return Catagory;
}
void Hourly::setCatagory(string catagory)
{
Catagory = catagory;
}
double Hourly::getAnnualSalary()
{
return AnnualSalary;
}
void Hourly::setAnnualSalary(double salary)
{
AnnualSalary = Hourly::CalculatePay() * 50;
}