On Microsoft VIsual Studio C++
need it in a C++ code that works also use the way that starts with hello world
Use the following UML diagram to build the class. The first section specifies the attributes. The second section specifies the behaviors, and the first character specifies the access modifier value, where:
“-” means that the class member is private, and
“+” means that the class member is public.
Listen
STEP 2: Code the Employee Class
Create a new project called “CIS247B_Week2Lab_YourName”.
Using the provided Class Diagram from Step 1, code the Employee class in the new project (i.e., “Realize the UML Class diagrams”).
The default constructor should set the attributes as follows: firstName = “not given”, lastName = “not given”, gender = “U” (for unknown), dependents = 0, and annualSalary = 20,000.
The multi-arg constructor should initialize all of the attributes using values passed in using its parameter list.
As shown in the Class diagram, each attribute should have a “getter” to retrieve the stored attribute value, and a “setter” that modifies the value.
The calculatePay( ) method of the Employee class should return the value of annual salary divided by 52 (return annualSalary / 52;).
The displayEmployee() method should display all the attributes of the Employee object in a well-formatted string with logical labels applied to each attribute. Don’t forget to call calculatePay from within the displayEmployee method in order to display the Employee’s weekly pay as well!
Listen
STEP 3: Code the Main Program
In the Main class, create code statements that perform the following operations. Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.).
Create an Employee object using the default constructor.
Prompt for and then set the first name, last name, gender, dependents, and annual salary. (Remember that you have to convert gender, dependents, and annual salary from strings to the appropriate data type.)
Using your code from last week, display a divider that contains the string “Employee Information”
Format the currency using the following formatting services:
Display the Employee information.
Create a second Employee object using the multi-argument constructor, setting each of the attributes with appropriate valid values.
Using your code from last week, display a divider that contains the string “Employee Information”.
Display the Employee information for the second Employee object.
Listen
STEP 4: Compile and Test
When done, compile and run your code.
Then, debug any errors until your code is error-free.
Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild.
Capture the Console output window and paste into a Word document. The following is a sample screen.
Expert Answer
#include <iostream>
#include <iomanip>
#include <string>
#include “Employee.h”
#include “Salaried.h”
#include “Hourly.h”
//function prototypes from Basic UI program week1
void DisplayApplicationInformation();
void DisplayDivider(string outputTitle);
string GetInput(string inputType);
void TerminateApplication();
int main()
{
DisplayApplicationInformation();
Employee employee1; //instantiate employee1 object using default constructor.
DisplayDivider(“Employee 1”);
//This section prompts for user input
string firstName = GetInput(“first name”);
employee1.setFirstName(firstName);
string lastName = GetInput(“last name”);
employee1.setLastName(lastName);
string gender = GetInput(“gender”);
char firstCharacterGender = gender[0]; //takes value of gender, places in array, and assigns first charcter to char firstCharacterGender.
employee1.setGender(firstCharacterGender);
string dependents = GetInput(“dependents”);
employee1.setDependents(dependents);
string annualSalary = GetInput(“annual salary”);
employee1.setAnnualSalary(annualSalary);
string healthInsurance = GetInput(“health insurance”);
employee1.getBenefit().setHealthInsurance(healthInsurance); //changed benefit to getBenefit. Make sure it works correctly.
string lifeInsurance = GetInput(“life insurance”);
employee1.getBenefit().setLifeInsurance(stod(lifeInsurance)); //changed benefit to getBenefit. Make sure it works correctly.
string vacation = GetInput(“vacation”);
employee1.getBenefit().setVacation(stoi(vacation)); //changed benefit to getBenefit. Make sure it works correctly.
employee1.displayEmployee();
cout << “n—-Number of Employee Objects Created—-” << endl;
cout << “Number of Employees: ” << Employee::getNumEmployees() << endl; //getNumEmployees called using class name.
//BEGIN section that creates a salaried employee object.
DisplayDivider(“Employee 2”);
Salaried salariedEmployee1;
//Enter the employee information to the object
firstName = GetInput(“first name”);
salariedEmployee1.setFirstName(firstName);
lastName = GetInput(“last name”);
salariedEmployee1.setLastName(lastName);
gender = GetInput(“gender”);
firstCharacterGender = gender[0];
salariedEmployee1.setGender(firstCharacterGender);
dependents = GetInput(“dependents”);
salariedEmployee1.setDependents(dependents);
annualSalary = GetInput(“annual salary”);
salariedEmployee1.setAnnualSalary(annualSalary);
healthInsurance = GetInput(“health insurance”);
salariedEmployee1.getBenefit().setHealthInsurance(healthInsurance);
lifeInsurance = GetInput(“life insurance”);
salariedEmployee1.getBenefit().setLifeInsurance(stod(lifeInsurance));
vacation = GetInput(“vacation”);
salariedEmployee1.getBenefit().setVacation(stoi(vacation));
salariedEmployee1.displayEmployee(); //display salaried employee information.
cout << “n—-Number of Employee Objects Created—-” << endl;
cout << “Number of Employees: ” << Employee::getNumEmployees() << endl; //getNumEmployees called using class name.
//END salaried employee section.
//BEGIN section that creates a hourly employee object.
DisplayDivider(“Employee 3”);
Hourly hourlyEmployee1;
//enter the employee information
firstName = GetInput(“first name”);
hourlyEmployee1.setFirstName(firstName);
lastName = GetInput(“last name”);
hourlyEmployee1.setLastName(lastName);
gender = GetInput(“gender”);
firstCharacterGender = gender[0];
hourlyEmployee1.setGender(firstCharacterGender);
dependents = GetInput(“dependents”);
hourlyEmployee1.setDependents(dependents);
annualSalary = GetInput(“annual salary”); //this section may need to be changed
hourlyEmployee1.setAnnualSalary();
healthInsurance = GetInput(“health insurance”);
hourlyEmployee1.getBenefit().setHealthInsurance(healthInsurance);
lifeInsurance = GetInput(“life insurance”);
hourlyEmployee1.getBenefit().setLifeInsurance(stod(lifeInsurance));
vacation = GetInput(“vacation”);
hourlyEmployee1.getBenefit().setVacation(stoi(vacation));
hourlyEmployee1.displayEmployee(); //display hourly employee.
cout << “n—-Number of Employee Objects Created—-” << endl;
cout << “Number of Employees: ” << Employee::getNumEmployees() << endl; //getNumEmployees called using class name.
//END hourly employee section.
TerminateApplication();
return 0;
}
//functions defined from Basic UI week 1 lab.
void DisplayApplicationInformation()
{
cout << “Welcome to the Employee Class Test Program.” << endl;
cout << “This program has been updated to include Salaried and Hourly class which inherit from Employee.” << endl;
}
void DisplayDivider(string outputTitle)
{
cout << ‘n’ << “******************************** ” + outputTitle + ” ***********************************” << endl;
}
string GetInput(string inputType)
{
cout << “Please enter ” + inputType + “: “;
string strInput;
getline(cin, strInput);
return strInput;
}
void TerminateApplication()
{
cout <<‘n’ << “Thank you for using the application!” << endl;
}
Employee.h
#ifndef EMPLOYEE_H //inclusion guard
#define EMPLOYEE_H
#include “Benefit.h”
using namespace std;
class Employee
{
protected: //protected attributes
string firstName;
string lastName;
char gender;
int dependents;
double annualSalary;
Benefit benefit; // may have to add setters and getters for this.
private:
static int numEmployees; //static variable keeps count of total number of employees.
public: //member functions
Employee(); //default constructor
Employee(string first, string last, char gen, int dep, Benefit benefit); //multi-arg constructor
double calculatePay();
void displayEmployee();
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
char getGender();
void setGender(char gen);
int getDependents();
void setDependents(int dep);
void setDependents(string dep);
double getAnnualSalary();
void setAnnualSalary(double salary);
void setAnnualSalary(string salary);
static int getNumEmployees();
Benefit getBenefit();
void setBenefit(Benefit ben);
};
#endif
Employee.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include “Employee.h”
Employee::Employee() //no-arg default constructor
{
firstName = “Not Given”;
lastName = “Not Given”;
gender = ‘U’;
dependents = 0;
annualSalary = 20000;
numEmployees++;
}
Employee::Employee(string first, string last, char gen, int dep, Benefit ben) : benefit(ben) //constructor with arguments
{
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
numEmployees++;
}
double Employee::calculatePay()
{
return annualSalary / 52;
}
void Employee::displayEmployee()
{
cout << “nEmployee Information” << endl;
cout << “————————————————” << endl;
cout << “Name: ” << firstName << ” ” << lastName << endl;
cout << “Gender: ” << gender << endl;
cout << “Dependents: ” << dependents << endl;
cout << “Annual Salary: $” << setprecision(2) << showpoint << fixed << annualSalary << endl;
cout << “Weekly Salary: $” << setprecision(2) << showpoint << fixed << calculatePay() << endl;
benefit.displayBenefits();
}
string Employee::getFirstName()
{
return firstName;
}
void Employee::setFirstName(string first)
{
firstName = first;
}
string Employee::getLastName()
{
return lastName;
}
void Employee::setLastName(string last)
{
lastName = last;
}
char Employee::getGender()
{
return gender;
}
void Employee::setGender(char gen)
{
gender = gen;
}
int Employee::getDependents()
{
return dependents;
}
void Employee::setDependents(int dep)
{
dependents = dep;
}
void Employee::setDependents(string dep)
{
dependents = stoi(dep); //string is converted to int.
}
double Employee::getAnnualSalary()
{
return annualSalary;
}
void Employee::setAnnualSalary(double salary)
{
annualSalary = salary;
}
void Employee::setAnnualSalary(string salary)
{
annualSalary = stod(salary); //string is converted to double.
}
Benefit Employee::getBenefit() //accessor function for benefit object in Employee.
{
return benefit;
}
void Employee::setBenefit(Benefit ben) //mutator function for benefit object in Employee.
{
benefit = ben;
}
int Employee::numEmployees = 0; //initialize static variable numEmployees to 0.
int Employee::getNumEmployees() //static member function
{
return numEmployees;
}
Hourly.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include “Hourly.h”
#include “Employee.h”
Hourly::Hourly() //may need to add and initialize constants here.
{
wage = 0;
hours = 0;
category = “Unknown”;
}
Hourly::Hourly(double wge, double hrs, string cat)
{
wage = wge;
hours = hrs;
category = cat;
}
Hourly::Hourly(string fname, string lname, char gen, int dep, double wge, double hrs, Benefit ben, string cat) : Employee(fname, lname, gen, dep, ben)
{
wage = wge;
hours = hrs;
category = cat;
}
double Hourly::calculatePay() //this should override function in Employee.
{
return wage * hours;
}
void Hourly::setAnnualSalary() //monitor this function may need to change.
{
annualSalary = calculatePay() * 50;
}
void Hourly::displayEmployee()
{
Employee::displayEmployee();
cout << “nHourly Employee” << endl;
cout << “Category: ” << category << endl;
cout << “Wage: ” << wage << endl;
cout << “Hours: ” << hours << endl;
}
Hourly.h
#include “Employee.h”
#ifndef HOURLY_H
#define HOURLY_H
using namespace std;
class Hourly : public Employee
{
private:
//UML diagram showed these as Doubles compiler would only allow int.
static const int MIN_WAGE = 10; //error message said “Error 12 error C2864: ‘Hourly::MIN_WAGE’ : only static const integral data members can be initialized within a class
static const int MAX_WAGE = 75;
static const int MIN_HOURS = 0;
static const int MAX_HOURS = 50;
double wage;
double hours;
string category;
public:
Hourly();
Hourly(double wage, double hours, string category);
Hourly(string fname, string lname, char gen, int dep, double wage, double hours, Benefit ben, string category);
double calculatePay();
void displayEmployee();
void setAnnualSalary(); //this was not in UML diagram, but lab instructions told us to override this function.
//setters and getters
double getWage();
void setWage();
double getHours();
void setHours();
string getCategory();
void setCategory();
};
#endif
salaried.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include “Salaried.h”
#include “Employee.h”
Salaried::Salaried()
{
managementLevel = 0;
}
Salaried::Salaried(string fname, string lname, char gen, int dep, double sal, Benefit ben, int manLevel) : Employee(fname, lname, gen, dep, ben)
{
managementLevel = manLevel;
annualSalary = sal;
}
Salaried::Salaried(double sal, int manLevel)
{
annualSalary = sal;
managementLevel = manLevel;
}
double Salaried::calculatePay()
{
return Employee::calculatePay() * (1 + (managementLevel * BONUS_PERCENT));
}
void Salaried::displayEmployee()
{
Employee::displayEmployee();
cout << “nSalaried Employee” << endl;
cout << “Management Level: ” << managementLevel << endl;
}
salaried.h
#include “Employee.h”
#ifndef SALARIED_H
#define SALARIED_H
using namespace std;
class Salaried : public Employee
{
private:
static const int MIN_MANAGEMENT_LEVEL = 0;
static const int MAX_MANAGEMENT_LEVEL = 3;
static const int BONUS_PERCENT = 10; //UML diagram showed this as a double??? and is was not a decimal in UML diagram.
int managementLevel;
public:
Salaried(); //default constructor
Salaried(string fname, string lname, char gen, int dep, double sal, Benefit ben, int manLevel); //multi-arg constructor
Salaried(double sal, int manLevel); //multi-arg constructor
double calculatePay();
void displayEmployee();
//setters and getters.
int getManagementLevel();
void setManagementLevel();
};
#endif
Benefit.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include “Benefit.h”
Benefit::Benefit() //default constructor
{
healthInsurance = “Not Given”;
lifeInsurance = 0.0;
vacation = 0;
}
Benefit::Benefit(string health, double life, int vaca) //multi-arg constructor
{
healthInsurance = health;
lifeInsurance = life;
vacation = vaca;
}
void Benefit::displayBenefits() //need to figure out how to right justify values.
{
cout << “nBenefit Information” << endl;
cout << “____________________________________________” << endl;
cout << “Health Insurance: ” << healthInsurance << endl;
cout << “Life Insurance: $” << setprecision(2) << showpoint << fixed << lifeInsurance << endl;
cout << “Vacation: ” << vacation << ” days” << endl;
}
string Benefit::getHealthInsurance()
{
return healthInsurance;
}
void Benefit::setHealthInsurance(string healthIns)
{
healthInsurance = healthIns;
}
double Benefit::getLifeInsurance()
{
return lifeInsurance;
}
void Benefit::setLifeInsurance(double lifeIns)
{
lifeInsurance = lifeIns;
}
int Benefit::getVacation()
{
return vacation;
}
void Benefit::setVacation(int vaca)
{
vacation = vaca;
}
Benefit.h
#ifndef BENEFIT_H
#define BENEFIT_H
using namespace std;
class Benefit
{
private:
string healthInsurance;
double lifeInsurance;
int vacation;
public:
Benefit(); //default constructor
Benefit(string health, double life, int vaca); //multi-arg constructor
void displayBenefits();
string getHealthInsurance();
void setHealthInsurance(string healthIns);
double getLifeInsurance();
void setLifeInsurance(double lifeIns);
int getVacation();
void setVacation(int vaca);
};
#endif