C++ program Employee object that hold first and last name, hire date, a code ind
ID: 3719894 • Letter: C
Question
C++ program
Employee object that hold first and last name, hire date, a code indicating whether the employee works ('w') or is a supervisor ('s'), and a field for yearly salary. Users should not be able to modify any of the properties directly.
class Employee{
//private members will not be accessible directly
private:
string fname;
string lname;
string hdate;
char code;
double salary;
//public constructor and mutator accessor methods prototypes
public:
Employee();
string getFname();
void setFname(string FNAME);
};
//constructor definition
Employee::Employee()
{
fname="Josh";
lname="Nichols";
hdate="29Mar2019";
code='w';
salary=2000;
}
//mutator and accessor method definition
void Employee :: setFname(string FNAME)
{
fname=FNAME;
}
string Employee :: getFname()
{
return fname;
}
Create a subclass of the "Employee" class called "Supervisor" with an additional property called "department" which is an integer number. Include a constructor and accessor and mutator methods for "department." The "Supervisor" class should have direct access to all of the properties of "Employee" (without calling accessor/mutator methods). List any changes that need to be made to your "Employee" class defined above to make this work. you can make anychange from the code
Write the code for the accessor and mutator methods above.
Explanation / Answer
#include <iostream>
using namespace std;
class Employee{
//private members will not be accessible directly
private:
string fname;
string lname;
string hdate;
char code;
double salary;
//public constructor and mutator accessor methods prototypes
public:
Employee();
string getFname();
void setFname(string FNAME);
void setLname(string LNAME);
void setHdate(string HDATE);
void setCode(char CODE);
void setSalary(double SALARY);
string getLname();
string getHdate();
char getCode();
double getSalary();
};
//constructor definition
Employee::Employee()
{
fname="Josh";
lname="Nichols";
hdate="29Mar2019";
code='w';
salary=2000;
}
//mutator and accessor method definition
void Employee :: setFname(string FNAME)
{
fname=FNAME;
}
string Employee :: getFname()
{
return fname;
}
void Employee ::setLname(string LNAME)
{
lname = LNAME;
}
void Employee ::setHdate(string HDATE)
{
hdate = HDATE;
}
void Employee ::setCode(char CODE)
{
code = CODE;
}
void Employee ::setSalary(double SALARY)
{
salary = SALARY;
}
string Employee ::getLname()
{
return lname;
}
string Employee ::getHdate()
{
return hdate;
}
char Employee ::getCode()
{
return code;
}
double Employee ::getSalary()
{
return salary;
}
class Supervisor : public Employee // derived class
{
private:
int department;
public:
Supervisor() //default constructor
{
setFname("Albert");
setLname("Jones");
setHdate("4Oct2015");
setCode('s');
setSalary(6000.00);
}
//set and get method for department
void setDepartment(int DEPARTMENT)
{
department = DEPARTMENT;
}
int getDepartment()
{
return department;
}
};
int main() {
Employee e;
cout<<"Employee Name : "<<e.getFname()<<" "<<e.getLname();
cout<<" Hire date : "<<e.getHdate();
cout<<" Salary : $"<<e.getSalary();
Supervisor s;
cout<<" Supervisor Name : "<<s.getFname()<<" "<<s.getLname();
cout<<" Hire date : "<<s.getHdate();
cout<<" Salary : $"<<s.getSalary();
return 0;
}
Output:
Employee Name : Josh Nichols
Hire date : 29Mar2019
Salary : $2000
Supervisor Name : Albert Jones
Hire date : 4Oct2015
Salary : $6000
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.