C++ I neeed the following added to my existing code: HERE IS THE UML DIAGRAM The
ID: 3860009 • Letter: C
Question
C++
I neeed the following added to my existing code:
HERE IS THE UML DIAGRAM
The only change to the Employee class is that there is a new attribute:
Notice that there is a "+" for this attribute, meaning that it is public. Make sure to examine the multi-arg constructor's signature!
Also, the dotted directed line between Employee and iEmployee specifies that the Employee class must implement the iEmployee abstract class, and thus provide an implementation for the calculatePay method.
1. Using the UML Diagram, create the Benefit class. .
2. Add a Benefit attribute to the Employee class.
3. Initialize the new Benefit attribute in both Employee constructors. Again, take note of the multi-arg constructors parameter list!
4. Create the iEmployee interface (abstract class in C++).
6. Modify the Employee class to call displayBenefit when displaying Employee information.
Notice that the Employee class now has a public benefit object inside it. This means that you can access the set methods of the Benefit object with the following code:
As an example, to set the lifeInsurance attribute inside an Employee object called emp, we could execute the following code:
A) Prompt for and set healthInsurance, lifeInsurance, and vacation.
B) Create a Benefit object called benefit1 using the multi-arg construction. Use any information you want for health insurance, life insurance, and vacation.
C) I need to add benefit1 to the Employee object and use the constructor.
"Mary", "Noia", 'F', 5, 24000.0, benefit1
THIS IS WHAT THE OUTPUT SHOULD LOOK LIKE:
THIS IS MY EXISTING CODE THAT NEEDS TO BE UPDATED:
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<sstream>
using namespace std;
class Employee
{
protected:
static int numEmployees;
//member variables of Employee class
private:
string firstName;
string lastName;
char gender;
int dependents;
double annualSalary;
public:
//default constructor
Employee()
{
//set the member variables to default values
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
//Incrementing number of employees
numEmployees += 1;
}
//constructor with parameters
Employee(string first, string last, char gen, int dep, double salary)
{
//set the member variables to values passed
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
//Incrementing number of employees
numEmployees += 1;
}
//getter methods
//method that gets firstName
string getfirstName()
{
return firstName;
}
//method that gets lastName
string getlastName()
{
return lastName;
}
//method that gets gender
char getGender()
{
return gender;
}
//method that gets dependents
int getdependents()
{
return dependents;
}
//method that gets annual salary
double getannualSalary()
{
return annualSalary;
}
//setter methods
//method that sets firstname
void setfirstName(string first)
{
firstName = first;
}
//method that sets lastname
void setlastName(string last)
{
lastName = last;
}
//method that sets gender
void setGender(char gen)
{
gender = gen;
}
//method that sets dependents
void setdependents(int dep)
{
dependents = dep;
}
//method that sets annual salary
void setannualSalary(double salary)
{
annualSalary = salary;
}
//Overloaded method that sets dependents
void setdependents(string dep)
{
dependents = stoi(dep);
}
//Overloaded method that sets annual salary
void setannualSalary(string salary)
{
annualSalary = stod(salary);
}
//method that calculates weekly pay
double calculatePay()
{
//calculate and return weekly pay
return annualSalary / 52;
}
//Static method
static int getNumEmployees()
{
//Return number of employee objects created
return numEmployees;
}
//method that displays employee information
void displayEmployee()
{
cout << " Employee Information ";
cout << "________________________________________________________________ ";
cout << " Name: " << firstName << " " << lastName;
cout << " Gender: " << gender;
cout << " Dependents: " << dependents;
cout << " Annual Salary: " << fixed << setprecision(2) << annualSalary;
cout << " Weekly Salary: " << fixed << setprecision(2) << calculatePay();
}
};
int Employee::numEmployees = 0;
int main()
{
//display banner
cout << "Welcome to your first Object Oriented Program--Employee ClassCIS247C, Week 3 Lab";
cout << "Name: ";
cout << " ******************** Employee 1 ******************** ";
//declare object for Employee
Employee emp1;
string fname, lname;
string gender, dependents, salstr;
double sal;
//prompt and read employee information
cout << "Please enter your First Name ";
cin >> fname;
cout << "Please enter your Last Name ";
cin >> lname;
cout << "Please enter your Gender ";
cin >> gender;
cout << "Please enter your Dependents ";
cin >> dependents;
cout << "Please enter your Annual Salary ";
cin >> salstr;
//set the employee information using setter methodds
emp1.setfirstName(fname);
emp1.setlastName(lname);
emp1.setGender(gender[0]);
/* Calling the new overloaded setters */
emp1.setdependents(dependents);
emp1.setannualSalary(salstr);
//display employee info
emp1.displayEmployee();
//Calling and displaying number of employee objects created
cout << " --- Number of Employee Object Created ---- ";
cout << "Number of Employees : " << Employee::getNumEmployees() << " ";
//create second object for Employee
//pass employee information as parameters
Employee emp2 = Employee("Mary", "Noia", 'F', 2, 150000);
cout << " ******************** Employee 2 ******************** ";
//display employee info
emp2.displayEmployee();
//Calling and displaying number of employee objects created
cout << " --- Number of Employee Object Created ---- ";
cout << "Number of Employees : " << Employee::getNumEmployees() << " ";
cout << " The end of the CIS247C Week4 iLab.";
return 0;
}
Explanation / Answer
Below is the code: -
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<sstream>
using namespace std;
class Benefit {
private:
string healthinsurance;
double lifeinsurance;
int vacation;
public:
Benefit() {
healthinsurance = "";
lifeinsurance = 0;
vacation = 0;
}
Benefit(string health,double life,int vaca) {
healthinsurance = health;
lifeinsurance = life;
vacation = vaca;
}
//method that displays employee information
void displayBenefits()
{
cout << " Benefit Information ";
cout << "________________________________________________________________ ";
cout << " Health Insurance: " << healthinsurance;
cout << " Life Insurance: " << fixed << setprecision(2)<<lifeinsurance;
cout << " Vacation: " << vacation<<" days";
}
void setHealthInsurance(string hins) {
healthinsurance = hins;
}
string getHealthInsurance() {
return healthinsurance;
}
void setLifeInsurance(double lifeins) {
lifeinsurance = lifeins;
}
double getLifeInsurance() {
return lifeinsurance;
}
void setVacation(int vaca) {
vacation = vaca;
}
int getVacation() {
return vacation;
}
};
class iEmployee {
public:
virtual double calculatePay() = 0;
};
class Employee : public iEmployee
{
protected:
static int numEmployees;
//member variables of Employee class
private:
string firstName;
string lastName;
char gender;
int dependents;
double annualSalary;
public:
Benefit benefit;
//default constructor
Employee()
{
//set the member variables to default values
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
//Incrementing number of employees
numEmployees += 1;
}
//constructor with parameters
Employee(string first, string last, char gen, int dep, double salary,Benefit benef)
{
//set the member variables to values passed
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
//Incrementing number of employees
numEmployees += 1;
benefit = benef;
}
//getter methods
//method that gets firstName
string getfirstName()
{
return firstName;
}
//method that gets lastName
string getlastName()
{
return lastName;
}
//method that gets gender
char getGender()
{
return gender;
}
//method that gets dependents
int getdependents()
{
return dependents;
}
//method that gets annual salary
double getannualSalary()
{
return annualSalary;
}
//setter methods
//method that sets firstname
void setfirstName(string first)
{
firstName = first;
}
//method that sets lastname
void setlastName(string last)
{
lastName = last;
}
//method that sets gender
void setGender(char gen)
{
gender = gen;
}
//method that sets dependents
void setdependents(int dep)
{
dependents = dep;
}
//method that sets annual salary
void setannualSalary(double salary)
{
annualSalary = salary;
}
//Overloaded method that sets dependents
void setdependents(string dep)
{
dependents = stoi(dep);
}
//Overloaded method that sets annual salary
void setannualSalary(string salary)
{
annualSalary = stod(salary);
}
//method that calculates weekly pay
double calculatePay()
{
//calculate and return weekly pay
return annualSalary / 52;
}
//Static method
static int getNumEmployees()
{
//Return number of employee objects created
return numEmployees;
}
//method that displays employee information
void displayEmployee()
{
cout << " Employee Information ";
cout << "________________________________________________________________ ";
cout << " Name: " << firstName << " " << lastName;
cout << " Gender: " << gender;
cout << " Dependents: " << dependents;
cout << " Annual Salary: " << fixed << setprecision(2) << annualSalary;
cout << " Weekly Salary: " << fixed << setprecision(2) << calculatePay();
cout<<" ";
benefit.displayBenefits();
}
};
int Employee::numEmployees = 0;
int main()
{
//display banner
cout << "Welcome to your first Object Oriented Program--Employee ClassCIS247C, Week 3 Lab";
cout << "Name: ";
cout << " ******************** Employee 1 ******************** ";
//declare object for Employee
Employee emp1;
string fname, lname;
string gender, dependents, salstr,healthins;
double sal,lifeinsur;
int vacation;
//prompt and read employee information
cout << "Please enter your First Name ";
cin >> fname;
cout << "Please enter your Last Name ";
cin >> lname;
cout << "Please enter your Gender ";
cin >> gender;
cout << "Please enter your Dependents ";
cin >> dependents;
cout << "Please enter your Annual Salary ";
cin >> salstr;
cout << "Please enter your Health insurance ";
cin >> healthins;
cout<< "Please enter your life insurance ";
cin>> lifeinsur;
cout<<" Please enter your vacation days ";
cin >> vacation;
emp1.benefit.setHealthInsurance(healthins);
emp1.benefit.setLifeInsurance(lifeinsur);
emp1.benefit.setVacation(vacation);
//set the employee information using setter methodds
emp1.setfirstName(fname);
emp1.setlastName(lname);
emp1.setGender(gender[0]);
/* Calling the new overloaded setters */
emp1.setdependents(dependents);
emp1.setannualSalary(salstr);
//display employee info
emp1.displayEmployee();
//Calling and displaying number of employee objects created
cout << " --- Number of Employee Object Created ---- ";
cout << "Number of Employees : " << Employee::getNumEmployees() << " ";
Benefit ben("North West Mutual",5000000,14);
//create second object for Employee
//pass employee information as parameters
Employee emp2 = Employee("Mary", "Noia", 'F', 2, 150000,ben);
cout << " ******************** Employee 2 ******************** ";
//display employee info
emp2.displayEmployee();
//Calling and displaying number of employee objects created
cout << " --- Number of Employee Object Created ---- ";
cout << "Number of Employees : " << Employee::getNumEmployees() << " ";
cout << " The end of the CIS247C Week4 iLab.";
return 0;
}
Sample Output:-
Welcome to your first Object Oriented Program--Employee ClassCIS247C, Week 3 Lab
Name:
******************** Employee 1 ********************
Please enter your First Name Nana
Please enter your Last Name Liu
Please enter your Gender Female
Please enter your Dependents 2
Please enter your Annual Salary 60000
Please enter your Health insurance Cigna
Please enter your life insurance 1.5
Please enter your vacation days 21
Employee Information
________________________________________________________________
Name: Nana Liu
Gender: F
Dependents: 2
Annual Salary: 60000.00
Weekly Salary: 1153.85
Benefit Information
________________________________________________________________
Health Insurance: Cigna
Life Insurance: 1.50
Vacation: 21 days
--- Number of Employee Object Created ----
Number of Employees : 1
******************** Employee 2 ********************
Employee Information
________________________________________________________________
Name: Mary Noia
Gender: F
Dependents: 2
Annual Salary: 150000.00
Weekly Salary: 2884.62
Benefit Information
________________________________________________________________
Health Insurance: North West Mutual
Life Insurance: 5000000.00
Vacation: 14 days
--- Number of Employee Object Created ----
Number of Employees : 2
The end of the CIS247C Week4 iLab.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.