1 Consider the following class class Employee private strin
1) Consider the following class:
class Employee
{
private:
string name;
const long officeNo;
const long empId;
int deptNo;
char empPosition; // ‘E’: entry level, ‘M’: manager, ‘D’: Director, ‘P’:Project_leader
int yearOfExp;
float salary;
static int totalNumofEmployees;
static int nextEmpId;
static int nextOfficeNo;
public:
Employee();
~Employee();
Employee(string theName, int theDeptNo, char theEmpPosition, int theYearOfExp);
void Print() const ;
void GetInfo();
}
The default constructor sets name to “Unknown”, Office Number to nextOfficeNo, Employee number to the value of nextEmpId, Department number to 0, Employee Position to Entry level, year of experience to 0, and salary to 0. It also ensures that the values of all static attributes are incremented by 1.
The second constructor sets the attributes based on what is passed to the function. The value of Employee Salary would still be set to 0 and the values of Office Number and Employee number are set to nextOfficeNo and nextEmpId, respectively. Again, the constructor should increment the values of all static attributes by 1.
The Print function should print all values of all attributes (regular, constants, and static) on the screen.
The GetInfo() function should prompt the user for the name, Employee position, and year of experience.
Create a c-style (stand alone) function called SetSalary (Employee&) that takes an employee object and sets the salary of the employee according to the following rules:
- If position is entry and number of experience is less than 2 years, sets the salary to 50000.
- If position is entry and number of experience is greater than or equal to 2 years, sets the salary to 55000.
- If position is project leader and number of experience is less than 4 years, sets the salary to 60000.
- If position is project leader and number of experience is greater than or equal to 4 years sets the salary to 65000.
- If position is manager, set the salary to 70000.
- If position is director, set the salary to 80000.
Make the SetSalary(Emplyee&) function to be the friend of class Employee so that it can access the private attributes in there.
The value of totalNumOfEmployees must be initialized to 0 before creating any object, be incremented upon creating each object of class Employee (in every constructor), and be decremented when an object of class Employee goes out of scope (in destructor).
The value of nextEmpId must be initialized to 1000 before creating any object, and must be incremented upon creating each object of class Employee in every constructor.
The value of nextOfficeNo must be initialized to 10 before creating any object, and must be incremented upon creating each object of class Employee in every constructor.
Write a driver program (main program ) to do the following:
• Create an object, emp1, which calls the default constructor right upon its creation. Then call the print function to print the information of emp1 on the screen
• Create another object, emp2, such the right upon its creation the employee name is set to “unknown”, the department number is set to 10, the employee office number becomes the value of nextOfficeNo, employee id becomes the value of nextEmpId, employee position becomes ‘?’, year of experience becomes 0.
• Use the object emp2 to call the GetInfo() function to prompt the user for setting name, employee position, and year of experience. Input “Adam Smith” for name, ‘P’ (project leader) for employee position and 7 for the year of experience.
• Call the SetSalary function and pass emp2 to the function to find and set the value of salary for emp2.
• Call the Print function to print the information of emp2 on the screen
Solution
below is the code as per your requirement :
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
string name;
long officeNo;
long empId;
int deptNo;
char empPosition; // ‘E’: entry level, ‘M’: manager, ‘D’: Director, ‘P’:Project_leader
int yearOfExp;
float salary;
static int totalNumofEmployees;
static int nextEmpId;
static int nextOfficeNo;
public:
Employee()
{
name=\"unknown\";
officeNo=nextOfficeNo;
empId=nextEmpId;
deptNo=0;
empPosition=\'E\';
yearOfExp=0;
salary=0.0;
totalNumofEmployees++;
nextEmpId++;
nextOfficeNo++;
}
~Employee()
{
totalNumofEmployees--;
}
Employee(string theName, int theDeptNo, char theEmpPosition, int theYearOfExp)
{
name=theName;
deptNo=theDeptNo;
empPosition=theEmpPosition;
yearOfExp=theYearOfExp;
officeNo=nextOfficeNo;
empId=nextEmpId;
salary=0.0;
totalNumofEmployees++;
nextEmpId++;
nextOfficeNo++;
}
void Print()
{
std::cout <<\"Latest updated records :\"<< std::endl;
cout<<\"\ \"<<\"totalNumofEmployees :\"<<totalNumofEmployees;
cout<<\"\ \"<<\"nextEmpId :\"<<nextEmpId;
cout<<\"\ \"<<\"nextOfficeNo :\"<<nextOfficeNo;
cout<<\"\ \"<<\"name :\"<<name;
cout<<\"\ \"<<\"deptNo :\"<<deptNo;
cout<<\"\ \"<<\"empPosition :\"<<empPosition;
cout<<\"\ \"<<\"empId :\"<<empId;
cout<<\"\ \"<<\"yearOfExp :\"<<yearOfExp;
cout<<\"\ \"<<\"officeNo :\"<<officeNo;
cout<<\"\ \"<<\"salary :\"<<salary;
}
void GetInfo()
{
cout<<\"enter employee name :\";
getline(cin, name);
cout<<\"enter the employee position (‘E’: entry level, ‘M’: manager, ‘D’: Director, ‘P’:Project_leader ):\";
cin>>empPosition;
cout<<\"enter the years of experience :\";
cin>>yearOfExp;
}
friend void SetSalary(Employee e);
};
int Employee::totalNumofEmployees=0;
int Employee::nextEmpId=1000;
int Employee::nextOfficeNo=10;
void SetSalary(Employee e)
{
if(e.empPosition==\'E\' && e.yearOfExp<2)
e.salary=50000;
else if(e.empPosition==\'E\' && e.yearOfExp>=2)
e.salary=55000;
else if(e.empPosition==\'P\' && e.yearOfExp<4)
e.salary=60000;
else if(e.empPosition==\'P\' && e.yearOfExp>=4)
e.salary=65000;
else if(e.empPosition==\'M\')
e.salary=70000;
else if(e.empPosition==\'D\')
e.salary=80000;
else
e.salary=0;
}
int main() {
Employee emp1; //default constructor
emp1.Print();
Employee emp2(\"unknown\",10,\'?\',0);
emp2.GetInfo();
SetSalary(emp2);
emp2.Print();
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------
feel free to reach out if you have any doubts. keep learning :)



