Class BasePlusCommissionEmployee
(Figs. 12.15–12.16) directly inherits from class CommissionEmployee
(line 9 of Fig. 12.15) and therefore is an indirect derived class of class Employee
. Class BasePlusCommissionEmployee
’s member-function implementations include a constructor (lines 9–15 of Fig. 12.16) that takes as arguments a first name, a last name, a social security number, a sales amount, a commission rate and a base salary. It then passes the first name, last name, social security number, sales amount and commission rate to the CommissionEmployee
constructor (line 12) to initialize the inherited members. BasePlusCommissionEmployee
also contains a set function (lines 18–24) to assign a new value to data member baseSalary
and a get function (lines 27–30) to return baseSalary
’s value. Function earnings
(lines 34–37) calculates a BasePlusCommissionEmployee
’s earnings. Line 36 in function earnings
calls base-class CommissionEmployee
’s earnings
function to calculate the commission-based portion of the employee’s earnings. This is another nice example of code reuse. BasePlusCommissionEmployee
’s print
function (lines 40–45) outputs "base-salaried"
, followed by the output of base-class CommissionEmployee
’s print
function (another example of code reuse), then the base salary. The resulting output begins with "base-salaried commission employee: "
followed by the rest of the BasePlusCommissionEmployee
’s information. Recall that CommissionEmployee
’s print
displays the employee’s first name, last name and social security number by invoking the print
function of its base class (i.e., Employee
)—yet another example of code reuse. BasePlusCommissionEmployee
’s print
initiates a chain of functions calls that spans all three levels of the Employee
hierarchy.
1 // Fig. 12.15: BasePlusCommissionEmployee.h
2 // BasePlusCommissionEmployee class derived from CommissionEmployee.
3 #ifndef BASEPLUS_H
4 #define BASEPLUS_H
5
6 #include <string> // C++ standard string class
7 #include "CommissionEmployee.h" // CommissionEmployee class definition
8
9 class BasePlusCommissionEmployee : public CommissionEmployee
10 {
11 public:
12 BasePlusCommissionEmployee( const std::string &, const std::string &,
13 const std::string &, double = 0.0, double = 0.0, double = 0.0 );
14 virtual ~CommissionEmployee() { } // virtual destructor
15
16 void setBaseSalary( double ); // set base salary
17 double getBaseSalary() const; // return base salary
18
19 // keyword virtual signals intent to override
20 virtual double earnings() const override; // calculate earnings
21 virtual void print() const override; // print object
22 private:
23 double baseSalary; // base salary per week
24 }; // end class BasePlusCommissionEmployee
25
26 #endif // BASEPLUS_H
1 // Fig. 12.16: BasePlusCommissionEmployee.cpp
2 // BasePlusCommissionEmployee member-function definitions.
3 #include <iostream>
4 #include <stdexcept>
5 #include "BasePlusCommissionEmployee.h"
6 using namespace std;
7
8 // constructor
9 BasePlusCommissionEmployee::BasePlusCommissionEmployee(
10 const string &first, const string &last, const string &ssn,
11 double sales, double rate, double salary )
12 : CommissionEmployee( first, last, ssn, sales, rate )
13 {
14 setBaseSalary( salary ); // validate and store base salary
15 } // end BasePlusCommissionEmployee constructor
16
17 // set base salary
18 void BasePlusCommissionEmployee::setBaseSalary( double salary )
19 {
20 if ( salary >= 0.0 )
21 baseSalary = salary;
22 else
23 throw invalid_argument( "Salary must be >= 0.0" );
24 } // end function setBaseSalary
25
26 // return base salary
27 double BasePlusCommissionEmployee::getBaseSalary() const
28 {
29 return baseSalary;
30 } // end function getBaseSalary
31
32 // calculate earnings;
33 // override virtual function earnings in CommissionEmployee
34 double BasePlusCommissionEmployee::earnings() const
35 {
36 return getBaseSalary() + CommissionEmployee::earnings();
37 } // end function earnings
38
39 // print BasePlusCommissionEmployee's information
40 void BasePlusCommissionEmployee::print() const
41 {
42 cout << "base-salaried ";
43 CommissionEmployee::print(); // code reuse
44 cout << "; base salary: " << getBaseSalary();
45 } // end function print
3.144.232.189