In C++ write a program with a class that has 5 names, 5 salaries, 5 hire dates (
ID: 3861567 • Letter: I
Question
In C++ write a program with a class that has 5 names, 5 salaries, 5 hire dates (at least the year). Your program should be able to list all records or one or more selected records. It should also calculate how many years since person was hired. Your program should be able to change data in selected records. In C++ write a program with a class that has 5 names, 5 salaries, 5 hire dates (at least the year). Your program should be able to list all records or one or more selected records. It should also calculate how many years since person was hired. Your program should be able to change data in selected records. In C++ write a program with a class that has 5 names, 5 salaries, 5 hire dates (at least the year). Your program should be able to list all records or one or more selected records. It should also calculate how many years since person was hired. Your program should be able to change data in selected records.Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <ctime>
using namespace std;
//Representing the employee information using class
class Employee
{
private:
string name;
int hiredYear;
int yearsWorked;
double salary;
public:
//constructor
Employee(string e_name, int e_hiredYear, double e_sal)
{
name = e_name;
hiredYear = e_hiredYear;
salary = e_sal;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year )- hiredYear;
}
//accessors and mutators
void setName(string e_name)
{
name = e_name;
}
void setSalary(double e_sal)
{
salary = e_sal;
}
void setHiredYear(int years)
{
hiredYear = years;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year) - years;
}
string getName()
{
return name;
}
double getSalary()
{
return salary;
}
int getYearsWorked()
{
return yearsWorked;
}
int getHiredYear()
{
return hiredYear;
}
void print()
{
cout<<" Name="<<name<<" HiredYear="<<hiredYear<<" Salary="<<salary;
cout<<" YearsWorked="<<yearsWorked<<endl;
}
};
int main() {
int choice, recordNum, updateChoice, hiredyear;
string name;
double sal;
//creating an array of classes
Employee employees[5]{{"John Smith", 2003, 4000},
{"Sheela Dixit", 1999, 6000},
{"Leena Barman", 2005, 3700},
{"James Moriairty", 2010, 2000},
{"John Watson", 1989, 8000}};
//using menu to complete the user actions
while(true)
{
cout<<"1. Print a record 2. Print all records 3. Update a record 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
employees[recordNum-1].print();
break;
case 2: for(int i=0; i<5; i++)
{
employees[i].print();
}
break;
case 3: cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
{
cout<<" 1. Update Name 2. Update salary 3. Update hired year";
cout<<" Enter your choice: ";
cin>>updateChoice;
if(updateChoice == 1)
{
cout<<"Enter the new name: ";
cin>>name;
employees[recordNum-1].setName(name);
}
else if(updateChoice == 2)
{
cout<<"Enter the new salary: ";
cin>>sal;
employees[recordNum-1].setSalary(sal);
}
else if(updateChoice == 3)
{
cout<<"Enter the new hired year: ";
cin>>hiredyear;
employees[recordNum-1].setHiredYear(hiredyear);
}
}
break;
case 4: exit(0);
}
}
return 0;
}
OUTPUT: