0133380165.pdf by Unknown

0133380165.pdf by Unknown

Author:Unknown
Language: eng
Format: epub
ISBN: 0133380165
Published: 2013-02-08T10:51:48+00:00


11.3 Relationship between Base and Derived Classes

507

Performance Tip 11.2

Using a member function to access a data member’s value can be slightly slower than ac-

cessing the data directly. However, today’s optimizing compilers are carefully designed to

perform many optimizations implicitly (such as inlining set and get member-function

calls). You should write code that adheres to proper software engineering principles, and

leave optimization to the compiler. A good rule is, “Do not second-guess the compiler.”

Changes to Class BasePlusCommissionEmployee ’s Member Function Definitions

Class BasePlusCommissionEmployee inherits CommissionEmployee’s public member

functions and can access the private base-class members via the inherited member func-

tions. The class’s header remains unchanged from Fig. 11.10. The class has several changes

to its member-function implementations (Fig. 11.15) that distinguish it from the previous

version of the class (Figs. 11.10–11.11). Member functions earnings (Fig. 11.15, lines

34–37) and print (lines 40–48) each invoke member function getBaseSalary to obtain

the base salary value, rather than accessing baseSalary directly. This insulates earnings

and print from potential changes to the implementation of data member baseSalary.

For example, if we decide to rename data member baseSalary or change its type, only

member functions setBaseSalary and getBaseSalary will need to change.

1

// Fig. 11.15: BasePlusCommissionEmployee.cpp

2

// Class BasePlusCommissionEmployee member-function definitions.

3

#include <iostream>

4

#include <stdexcept>

5

#include "BasePlusCommissionEmployee.h"

6

using namespace std;

78 // constructor

9

BasePlusCommissionEmployee::BasePlusCommissionEmployee(

10

const string &first, const string &last, const string &ssn,

11

double sales, double rate, double salary )

12

// explicitly call base-class constructor

13

: CommissionEmployee( first, last, ssn, sales, rate )

14

{

15

setBaseSalary( salary ); // validate and store base salary

16

} // end BasePlusCommissionEmployee constructor

17

18

// set base salary

19

void BasePlusCommissionEmployee::setBaseSalary( double salary )

20

{

21

if ( salary >= 0.0 )

22

baseSalary = salary;

23

else

24

throw invalid_argument( "Salary must be >= 0.0" );

25

} // end function setBaseSalary

26

27

// return base salary

28

double BasePlusCommissionEmployee::getBaseSalary() const

29

{

Fig. 11.15 | BasePlusCommissionEmployee class that inherits from class

CommissionEmployee but cannot directly access the class’s private data. (Part 1 of 2.)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.