Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee of the CommissionEmployee–BasePlusCom- missionEmployee hierarchy to use composition rather than inheritance. After you do this, assess the relative merits of the two approaches for designing classes CommissionEmployee and BasePlusCom- missionEmployee, as
well as for object-oriented programs in general. Which approach is more nat- ural? Why?
What will be an ideal response?
For a relatively short program like this one, either approach is acceptable. Inheritance becomes preferable when it makes the program easier to modify and promotes the reuse of code. The inheritance approach is more natural because a base-salaried commission employee is a commission employee. Composition is defined by the “has-a” relationship, and clearly it would be strange to say that “a base-salaried commission employee has a commission employee.”
```
// BasePlusCommissionEmployee class using composition.
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include
#include "CommissionEmployee.h" // CommissionEmployee class definition
using namespace std;
class BasePlusCommissionEmployee
{
public:
BasePlusCommissionEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0, double = 0.0 );
void setFirstName( const string & ); // set first name
string getFirstName() const; // return first name
void setLastName( const string & ); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber( const string & ); // set SSN
string getSocialSecurityNumber() const; // return SSN
void setGrossSales( double ); // set gross sales amount
double getGrossSales() const; // return gross sales amount
void setCommissionRate( double ); // set commission rate
double getCommissionRate() const; // return commission rate
void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary
double earnings() const; // calculate earnings
void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary
CommissionEmployee commissionEmployee; // composed object
}; // end class BasePlusCommissionEmployee
#endif
```
```
// Member-function definitions of class BasePlusCommissionEmployee
// using composition.
#include
// BasePlusCommissionEmployee class definition
#include "BasePlusCommissionEmployee.h"
using namespace std;
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
// initialize composed object
: commissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set commission employee's first name
void BasePlusCommissionEmployee::setFirstName( const string &first )
{
commissionEmployee.setFirstName( first );
} // end function setFirstName
// return commission employee's first name
string BasePlusCommissionEmployee::getFirstName() const
{
return commissionEmployee.getFirstName();
} // end function getFirstName
// set commission employee's last name
void BasePlusCommissionEmployee::setLastName( const string &last )
{
commissionEmployee.setLastName( last );
} // end function setLastName
// return commission employee's last name
string BasePlusCommissionEmployee::getLastName() const
{
return commissionEmployee.getLastName();
} // end function getLastName
// set commission employee's social security number
void BasePlusCommissionEmployee::setSocialSecurityNumber(
const string &ssn )
{
commissionEmployee.setSocialSecurityNumber( ssn );
} // end function setSocialSecurityNumber
// return commission employee's social security number
string BasePlusCommissionEmployee::getSocialSecurityNumber() const
{
return commissionEmployee.getSocialSecurityNumber();
} // end function getSocialSecurityNumber
// set commission employee's gross sales amount
void BasePlusCommissionEmployee::setGrossSales( double sales )
{
commissionEmployee.setGrossSales( sales );
} // end function setGrossSales
// return commission employee's gross sales amount
double BasePlusCommissionEmployee::getGrossSales() const
{
return commissionEmployee.getGrossSales();
} // end function getGrossSales
// set commission employee's commission rate
void BasePlusCommissionEmployee::setCommissionRate( double rate )
{
commissionEmployee.setCommissionRate( rate );
} // end function setCommissionRate
75 // return commission employee's commission rate
double BasePlusCommissionEmployee::getCommissionRate() const
{
return commissionEmployee.getCommissionRate();
} // end function getCommissionRate
// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings
double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + commissionEmployee.earnings();
} // end function earnings
// print BasePlusCommissionEmployee object
void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried ";
// invoke composed CommissionEmployee object's print function
commissionEmployee.print();
cout << "\nbase salary: " << getBaseSalary();
} // end function print
```
You might also like to view...
The Font effect ________ provides the user with Embossed and Engraved options
Fill in the blank(s) with correct word
__________ is a type of relationship participation.
a. Mandatory b. Optional c. Both mandatory and optional d. Neither mandatory nor optional