-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommissionEmployee.cpp
More file actions
56 lines (47 loc) · 1.57 KB
/
CommissionEmployee.cpp
File metadata and controls
56 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Fig. 12.14: CommissionEmployee.cpp
// CommissionEmployee class member-function definitions.
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include "CommissionEmployee.h" // CommissionEmployee class definition
using namespace std;
// constructor
CommissionEmployee::CommissionEmployee(const string& first,
const string& last, const string& ssn, double sales, double rate)
: Employee(first, last, ssn) {
setGrossSales(sales);
setCommissionRate(rate);
}
// set gross sales amount
void CommissionEmployee::setGrossSales(double sales) {
if (sales < 0.0) {
throw invalid_argument("Gross sales must be >= 0.0");
}
grossSales = sales;
}
// return gross sales amount
double CommissionEmployee::getGrossSales() const { return grossSales; }
// set commission rate
void CommissionEmployee::setCommissionRate(double rate) {
if (rate <= 0.0 || rate > 1.0) {
throw invalid_argument("Commission rate must be > 0.0 and < 1.0");
}
commissionRate = rate;
}
// return commission rate
double CommissionEmployee::getCommissionRate() const {
return commissionRate;
}
// calculate earnings; override pure virtual function earnings in Employee
double CommissionEmployee::earnings() const {
return getCommissionRate() * getGrossSales();
}
// return a string representation of CommissionEmployee's information
string CommissionEmployee::toString() const {
ostringstream output;
output << fixed << setprecision(2);
output << "commission employee: " << Employee::toString()
<< "\ngross sales: " << getGrossSales()
<< "; commission rate: " << getCommissionRate();
return output.str();
}