Need help with c++ h file. #include <string> #include <iostream> #include <set>
ID: 3721341 • Letter: N
Question
Need help with c++ h file.
#include <string>
#include <iostream>
#include <set>
#include <vector>
#include <queue>
class Employee {
public:
/***
* Constructor needs employee name
*/
explicit Employee(std::string name);
/***
* return a copy of the name
*/
std::string getName() const;
/***
* return the pay due
*/
virtual double getPay() const = 0;
/***
* create a string from employee information
* should output
* "Name = <name_>"
*/
virtual std::string toString() const;
private:
/***
* name of the employee
*/
std::string name_;
friend std::ostream &operator<<(std::ostream &, const Employee &e);
};
std::ostream &operator<<(std::ostream &, const Employee &e);
class HourlyEmployee : public Employee {
using Employee::Employee;
public:
virtual double getPay() const;
void setHourlyRate(double rate);
void setHoursWorked(double hours);
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const;
private:
double hourly_rate_;
double hours_worked_;
friend std::ostream &operator<<(std::ostream &, const HourlyEmployee &e);
};
std::ostream &operator<<(std::ostream &, const HourlyEmployee &e);
class CommissionedEmployee : public Employee {
using Employee::Employee;
public:
virtual double getPay() const;
void setBaseSalary(double dollars);
void setCommissionRate(double rate);
void setSalesVolume(double dollars);
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const;
private:
double commission_rate_;
double base_salary_;
double sales_volume_;
friend std::ostream &operator<<(std::ostream &,
const CommissionedEmployee &e);
};
std::ostream &operator<<(std::ostream &, const CommissionedEmployee &e);
class SalariedEmployee : public Employee {
using Employee::Employee;
public:
virtual double getPay() const;
void setSalary(double salary);
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const;
private:
double salary_;
friend std::ostream &operator<<(std::ostream &, const SalariedEmployee &e);
};
std::ostream &operator<<(std::ostream &, const SalariedEmployee &e);
Explanation / Answer
//employee.h
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
#include <set>
#include <vector>
#include <queue>
class Employee {
public:
/***
* Constructor needs employee name
*/
explicit Employee(std::string name)
{
name_ = name;
}
/***
* return a copy of the name
*/
std::string getName() const
{
return name_;
}
/***
* return the pay due
*/
virtual double getPay() const = 0;
/***
* create a string from employee information
* should output
* "Name = <name_>"
*/
virtual std::string toString() const
{
return("Name = "+name_);
}
private:
/***
* name of the employee
*/
std::string name_;
friend std::ostream &operator<<(std::ostream &, const Employee &e);
};
std::ostream &operator<<(std::ostream &dout, const Employee &e)
{
dout<<e.toString();
return dout;
}
class HourlyEmployee : public Employee {
using Employee::Employee;
public:
virtual double getPay() const
{
return(hours_worked_*hourly_rate_);
}
void setHourlyRate(double rate)
{
hourly_rate_ = rate;
}
void setHoursWorked(double hours)
{
hours_worked_ = hours;
}
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const
{
return(Employee::toString()+ " Hours Worked : "+std::to_string(hours_worked_) + " Hourly Rate : "+std::to_string(hourly_rate_)+" Pay : "+std::to_string(getPay()));
}
private:
double hourly_rate_;
double hours_worked_;
friend std::ostream &operator<<(std::ostream &, const HourlyEmployee &e);
};
std::ostream &operator<<(std::ostream &dout, const HourlyEmployee &e)
{
dout<<e.toString();
return dout;
}
class CommissionedEmployee : public Employee {
using Employee::Employee;
public:
// assuming rate is in percentage
virtual double getPay() const
{
return(base_salary_ + ((commission_rate_*sales_volume_)/100));
}
void setBaseSalary(double dollars)
{
base_salary_= dollars;
}
void setCommissionRate(double rate)
{
commission_rate_ = rate;
}
void setSalesVolume(double dollars)
{
sales_volume_ = dollars;
}
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const
{
return(Employee::toString() + " Base salary : "+std::to_string(base_salary_)+" Commission Rate : "+std::to_string(commission_rate_)+ " Pay : "+std::to_string(getPay()));
}
private:
double commission_rate_;
double base_salary_;
double sales_volume_;
friend std::ostream &operator<<(std::ostream &, const CommissionedEmployee &e);
};
std::ostream &operator<<(std::ostream &dout, const CommissionedEmployee &e)
{
dout<<e.toString();
return dout;
}
class SalariedEmployee : public Employee {
using Employee::Employee;
public:
virtual double getPay() const
{
return salary_;
}
void setSalary(double salary)
{
salary_ = salary;
}
/***
* create a string from employee information
* should output
* information as in the test result file "
*/
std::string toString() const
{
return(Employee::toString()+" Salary : "+std::to_string(salary_));
}
private:
double salary_;
friend std::ostream &operator<<(std::ostream &, const SalariedEmployee &e);
};
std::ostream &operator<<(std::ostream &dout, const SalariedEmployee &e)
{
dout<<e.toString();
return dout;
}
#endif /* EMPLOYEE_H_ */
//end of employee.h