-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.9_main.cpp
More file actions
79 lines (66 loc) · 2.84 KB
/
12.9_main.cpp
File metadata and controls
79 lines (66 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Fig. 12.17: fig12_17.cpp
// Processing Employee drived-class objects with static binding
// then polymorphically cusing dyanmic binding.
#include <iostream>
#include <iomanip>
#include <vector>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
#include "Date.h"
using namespace std;
void virtualViaPointer(const Employee* const); // prototype
void virtualViaReference(const Employee&); // prototype
int main() {
cout << fixed << setprecision(2); // set floating-point formatting
// create birth data of employees
Date johnbirth(11, 5, 1996);
Date suebirth(11, 2, 2000);
Date bobbirth(11, 30, 1990);
// create derived-class objects
SalariedEmployee salariedEmployee{
"John", "Smith", "111-11-1111", johnbirth, 800 };
CommissionEmployee commissionEmployee{
"Sue", "Jones", "333-33-3333", suebirth, 10000, .06 };
BasePlusCommissionEmployee basePlusCommissionEmployee{
"Bob", "Lewis", "444-44-4444", bobbirth, 5000, .04, 300 };
// output each Employee's information and earnings using static binding
cout << "EMPLOYEES PROCESSED INDIVIDUALLY USING STATIC BINDING\n"
<< "If the current month(11) is the month in which the Employee's birhtday occurs,\n"
<< "add $100.00 bonus to the Employee's payroll"
<< salariedEmployee.toString()
<< "\nearned $" << salariedEmployee.earnings() << "\n\n"
<< commissionEmployee.toString()
<< "\nearned $" << commissionEmployee.earnings() << "\n\n"
<< basePlusCommissionEmployee.toString()
<< "\nearned $" << basePlusCommissionEmployee.earnings() << "\n\n";
// create and initialize vector of three base-class pointers
vector<Employee*> employees{ &salariedEmployee, &commissionEmployee,
&basePlusCommissionEmployee };
cout << "EMPLOYEES PROCESSED POLYMORPHICALLY VIA DYNAMIC BINDING\n\n";
// call virtualViaPointer to print each Employee's information
// and earnings using dynamic binding
cout << "VIRTUAL FUNCTION CALLS MADE OFF BASE-CLASS POINTERS\n";
for (const Employee* employeePtr : employees) {
virtualViaPointer(employeePtr);
}
// call virtualViaReference to print each Employee's information
// and earnings using dynamic binding
cout << "VIRTUAL FUNCTION CALLS MADE OFF BASE-CLASS REFERENCES\n";
for (const Employee* employeePtr : employees) {
virtualViaReference(*employeePtr); // note dereferencing
}
}
// call Employee virtual functions toString and earnings off a
// base-class pointer using dynamic binding
void virtualViaPointer(const Employee* const baseClassPtr) {
cout << baseClassPtr->toString()
<< "\nearned $" << baseClassPtr->earnings() << "\n\n";
}
// call Employee virtual functions toString and earnings off a
// base-class reference using dynamic binding
void virtualViaReference(const Employee& baseClassRef) {
cout << baseClassRef.toString()
<< "\nearned $" << baseClassRef.earnings() << "\n\n";
}