C++11 for Programmers by Paul J. Deitel & Harvey M. Deitel

C++11 for Programmers by Paul J. Deitel & Harvey M. Deitel

Author:Paul J. Deitel & Harvey M. Deitel [Deitel, Paul J. & Deitel, Harvey M.]
Language: eng
Format: epub
Tags: Computer Science, Education & Reference, Computers & Technology, Programming Languages, C#, Languages & Tools, Programming, C
ISBN: 9780133439854
Publisher: Prentice Hall
Published: 2013-03-22T14:00:00+00:00


* * *

Fig. 12.13. CommissionEmployee class header.

Click here to view code image

* * *

1 // Fig. 12.14: CommissionEmployee.cpp

2 // CommissionEmployee class member-function definitions.

3 #include <iostream>

4 #include <stdexcept>

5 #include "CommissionEmployee.h" // CommissionEmployee class definition

6 using namespace std;

7

8 // constructor

9 CommissionEmployee::CommissionEmployee( const string &first,

10 const string &last, const string &ssn, double sales, double rate )

11 : Employee( first, last, ssn )

12 {

13 setGrossSales( sales );

14 setCommissionRate( rate );

15 } // end CommissionEmployee constructor

16

17 // set gross sales amount

18 void CommissionEmployee::setGrossSales( double sales )

19 {

20 if ( sales >= 0.0 )

21 grossSales = sales;

22 else

23 throw invalid_argument( "Gross sales must be >= 0.0" );

24 } // end function setGrossSales

25

26 // return gross sales amount

27 double CommissionEmployee::getGrossSales() const

28 {

29 return grossSales;

30 } // end function getGrossSales

31

32 // set commission rate

33 void CommissionEmployee::setCommissionRate( double rate )

34 {

35 if ( rate > 0.0 && rate < 1.0 )

36 commissionRate = rate;

37 else

38 throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" );

39 } // end function setCommissionRate

40

41 // return commission rate

42 double CommissionEmployee::getCommissionRate() const

43 {

44 return commissionRate;

45 } // end function getCommissionRate

46

47 // calculate earnings; override pure virtual function earnings in Employee

48 double CommissionEmployee::earnings() const

49 {

50 return getCommissionRate() * getGrossSales();

51 } // end function earnings

52

53 // print CommissionEmployee's information

54 void CommissionEmployee::print() const

55 {

56 cout << "commission employee: ";

57 Employee::print(); // code reuse

58 cout << "\ngross sales: " << getGrossSales()

59 << "; commission rate: " << getCommissionRate();

60 } // end function print



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.