Answered step by step
Verified Expert Solution
Question
1 Approved Answer
[c++] modify this code to: Create another employee with a parameter constructor and display the employee information. Reassign an employee to a different department and
[c++] modify this code to:
- Create another employee with a parameter constructor and display the employee information.
- Reassign an employee to a different department and role with increased salary and display the result.
/* * Employee.h file */ #pragma once #include#include #include #include #include using namespace std; #define SIZE 5 enum Role { programmer = 0, manager = 1, director = 2 }; class Employee { string firstName; string lastName; int SSN; string department; Role role; double salary; public: static int count; Employee(); void setSalary(double sala); void setRoles(Role r); Role getRole(); //Added by CHEGGEA //add mutator function to set private variables void setFirstName(string str); void setLastName(string str); void setSSN(int ss); void setDepartment(string dept); //add accessor functions to access private variable string getFirstName(); string getLastName(); int getSSN(); string getDepartment(); double getSalary(); }; /* Employee.h file ends here */ /* * Employee.cpp file */ #include"Employee.h" //Added constructor without which you cannot create an object,CHEGGEA Employee::Employee() { firstName = ""; lastName = ""; SSN = 0; department = ""; role = Role(programmer); salary = 0; count++; } //Added new function by CHEGGEA //add mutator function to set private variables void Employee::setFirstName(string str) { firstName = str; } void Employee::setLastName(string str) { lastName = str; } void Employee::setSSN(int ss) { SSN = ss; } void Employee::setDepartment(string dept) { department = dept; } //add accessor functions to access private variable string Employee::getFirstName() { return firstName; } string Employee::getLastName() { return lastName; } int Employee::getSSN() { return SSN; } string Employee::getDepartment() { return department; } void Employee::setSalary(double sala) { salary = sala; } void Employee::setRoles(Role r) { role = r; } Role Employee::getRole() { return role; } double Employee::getSalary() { return salary; } /* Employee.cpp file ends here */ /* * Main.cpp file */ #include #include #include "Employee.h" using namespace std; void hardCoding(Employee *em) { em[0].setFirstName("Bob"); em[0].setLastName("Marley"); em[0].setDepartment("Acc."); em[0].setSSN(3); em[1].setFirstName("Shas"); em[1].setLastName("Ivy"); em[1].setDepartment("Audit"); em[1].setSSN(4); em[2].setFirstName("Margo"); em[2].setLastName("Kim"); em[2].setDepartment("Mgmnt."); em[2].setSSN(5); em[3].setFirstName("Milly"); em[3].setLastName("Pink"); em[3].setDepartment("IT"); em[3].setSSN(6); em[4].setFirstName("Sia"); em[4].setLastName("Miskel"); em[4].setDepartment("IT"); em[4].setSSN(7); } //Modified functions by CHEGGEA void setSalaries(Employee *em, int size) { for (int i = 0; i < size; i++) { em[i].setSalary(rand() % 100000); } } void increaseSalaries(Employee *em, int size, int rate, int role) { for (int i = 0; i < size; i++) { if(em[i].getRole() == role) em[i].setSalary(em[i].getSalary() + (em[i].getSalary() * rate * 0.01)); } } void setRoles(Employee *em, int size) { for (int i = 0; i < size; i++) { em[i].setRoles(Role(rand() % 3)); } } string printRole(Role role) { string ret; switch (role) { case 0: ret = "Programmer"; break; case 1: ret = "Manager"; break; case 2: ret = "Director"; break; } return ret; } double getAveargeSalary(Employee *em, int size) { double avgSal = 0.0; for (int i = 0; i < SIZE; i++) { avgSal += em[i].getSalary(); } return avgSal/size; } void displayByRoles(Employee *em, int size, int role) { cout << endl << "Details of " << printRole(Role(role)) << endl; for (int i = 0; i < Employee::count; i++) { if(em[i].getRole() == 0) { cout << em[i].getFirstName() << " " << em[i].getLastName() << " "; cout << em[i].getSSN() << " "; cout << em[i].getDepartment() << " "; cout << printRole(em[i].getRole()) << " "; cout << em[i].getSalary() << endl; } } } void displayDatabase(Employee *em, int size) { cout << "Name SSN Dapartment Role Salary" << endl; for (int i = 0; i < SIZE; i++) { cout << em[i].getFirstName() << " " << em[i].getLastName() << " "; cout << em[i].getSSN() << " "; cout << em[i].getDepartment() << " "; cout << printRole(em[i].getRole()) << " "; cout << em[i].getSalary() << endl; } } int Employee::count = 0; int main() { Employee *emp = new Employee[SIZE]; srand((unsigned)time(NULL)); hardCoding(emp); setSalaries(emp, Employee::count); setRoles(emp, Employee::count); cout << endl << "Employee Database "; displayDatabase(emp, Employee::count); cout << endl << "Average Salary: " << getAveargeSalary(emp, Employee::count) << endl; displayByRoles(emp, Employee::count, 0); increaseSalaries(emp, Employee::count, rand()%10, 1); cout << endl << "Employee Database after incrementing salaries of all the managers" << endl << endl; displayDatabase(emp, Employee::count); cout << endl << "The numbers of Employees : " << Employee::count << endl; return 0; } /* Main.cpp file ends */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started