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
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.
Adulting by Kelly Williams Brown(4092)
Figure Drawing for Artists by Steve Huston(3152)
Drawing Cutting Edge Anatomy by Christopher Hart(3128)
Draw Your Day by Samantha Dion Baker(2979)
Drawing Shortcuts: Developing Quick Drawing Skills Using Today's Technology by Leggitt Jim(2810)
Make Comics Like the Pros by Greg Pak(2645)
Rapid Viz: A New Method for the Rapid Visualization of Ideas by Kurt Hanks & Larry Belliston(2596)
Draw to Win: A Crash Course on How to Lead, Sell, and Innovate With Your Visual Mind by Dan Roam(2524)
How Proust Can Change Your Life by Alain De Botton(2496)
How The Mind Works by Steven Pinker(2484)
0041152001443424520 .pdf by Unknown(2475)
Day by Elie Wiesel(2463)
Modern Cartooning by Christopher Hart(2394)
Tattoo Art by Doralba Picerno(2375)
Draw-A-Saurus by James Silvani(2366)
Poses for Artists Volume 2 - Standing Poses: An essential reference for figure drawing and the human form. (Inspiring Art and Artists) by Justin Martin(2328)
Learn Drawing Quickly by Sharon Finmark(2301)
Poses for Artists - Dynamic & Sitting: An essential reference for figure drawing and the human form (Inspiring Art and Artists Book 1) by Justin R Martin(2241)
Drawing and Painting Birds by Tim Wootton(2213)
