The objective of the lab is to modify the Employee class to demonstrate composit
ID: 3633736 • Letter: T
Question
The objective of the lab is to modify the Employee class to demonstrate composition and a class interface. An employee typically has benefits, so we will make the following changes:
Due this week:
STEP 1: Understand 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.
STEP 2: Create the Project
You will want to use the Week 3 project as the starting point for the lab. To do this, you will want to create a new project by following these steps:
STEP 3: Modify the Employee Class
STEP 4: Modify the Main Method
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:
The steps required to modify the Main class are below. New steps are in bold.
STEP 5: 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.
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Employee
{
private:
string firstName;
string lastName;
string gender;
int dependents;
double salary;
double CalculatePay();
static int numEmployees;
public:
Employee()
{
firstName="Not-Given";
lastName="Not-Given";
gender="U";
dependents=0;
salary=20000;
numEmployees +=1;
}
Employee(string firstName,string lastName,
string gender,int dependents,double salary)
{
numEmployees +=1;
this->firstName =firstName;
this->lastName =lastName;
this->gender =gender;
this->dependents =dependents;
this->salary =salary;
}
void getData()
{
cout<<"Enter firstName ";
cin>>firstName;
setFirstName(firstName);
cout<<"Enter lastName ";
cin>>lastName;
setLastName(lastName);
cout<<"Enter Gender ";
cin>>gender;
setGender(gender);
cout<<"Enter Dependents ";
cin>>dependents;
setDependents(dependents);
cout<<"Enter Salary ";
cin>>salary;
setAnnualSalary(salary);
}
void setFirstName(string firstName)
{
this->firstName=firstName;
}
void setLastName(string lastName)
{
this->lastName =lastName;
}
void setGender(string gender)
{
this->gender =gender;
}
void setDependents(int dependents)
{
this->dependents =dependents;
}
void setAnnualSalary(double salary)
{
this->salary =salary;
}
//getter methods
string getFirstName()
{
return firstName;
}
string getLastName()
{
return lastName;
}
string getGender()
{
return gender;
}
int getDependents()
{
return dependents;
}
double getAnnualSalary()
{
return salary;
}
//To calculate
double calculatePay()
{
return getAnnualSalary()/52;
}
void display()
{
displayDivider();
cout<<setw(15)<<"FirstName :"<<getFirstName()<<endl;
cout<<setw(15)<<"LastName :"<<getLastName()<<endl;
cout<<setw(15)<<"Gender :"<<getGender()<<endl;
cout<<setw(15)<<"Dependents :"<<getDependents()<<endl;
cout<<setw(15)<<setprecision(2)<<showpoint<<fixed<<
"AnnualSalary :"<<getAnnualSalary()<<endl;
cout<<setw(15)<<setprecision(2)<<showpoint<<fixed<<
"Weekly Pay:" <<calculatePay()<<endl<<endl;
}
void displayDivider()
{
cout<<" EMPLOYEE INFORMATION"<<endl;
cout<<"****************************************"<<endl;
}
};
{
//Benefit.cs
class Benefits
{
// data fields
private string healthInsurance;
private double lifeInsurance;
private int vacation;
//default constructor
public Benefit()
{
}
//argumented constructor
public Benefit(string health, double life, int v)
{
healthInsurance = health;
lifeInsurance = life;
vacation = v;
}
//displays the data of the object
public void DisplayBenefits()
{
Console.WriteLine("Health Insurance: {0}", healthInsurance);
Console.WriteLine("Life Insurance: {0}", lifeInsurance);
Console.WriteLine("Vacation: {0}", vacation);
}
//sets health insurance
public void SetHealthInsurance(string health)
{
healthInsurance = health;
}
//returns health insurance
public string GetHealthInsurance()
{
return healthInsurance;
}//sets life insurance
public void SetLifeInsurance(double life)
{
lifeInsurance = life;
}//returns life insurance
public double GetLifeInsurance()
{
return lifeInsurance;
}//sets vacation
public void SetVacation(int v)
{
vacation = v;
}//returns vacation
public int GetVacation()
{
return vacation;
}
static int getNumEmployees()
{
return numEmployees;
}
};
int Employee::numEmployees=0;
int main()
{
Employee obj;
obj.getData();
obj.display ();
Employee obj2("Mary", "Noia", "F", 5, 24000.0);
obj2.display();
int numEmp=Employee::getNumEmployees();
cout<<"Number of employees :"<<numEmp<<endl;
system("pause");
return 0;
}
}
Example of what it's suppose to look like:
i L A B O V E R V I E WExplanation / Answer
To view the answer go to http://goo.gl/Fu6hE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.