-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualfunctionexample.cpp
More file actions
78 lines (65 loc) · 1.36 KB
/
virtualfunctionexample.cpp
File metadata and controls
78 lines (65 loc) · 1.36 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
#include <iostream>
using namespace std;
class Employee
{
public:
virtual void raiseSalary()
{
cout << "Employee salary raised (general)" << endl;
}
virtual void promote()
{
cout << "Employee promoted (general)" << endl;
}
// virtual destructor
virtual ~Employee()
{
}
};
// Derived class: Manager
class Manager : public Employee
{
public:
void raiseSalary() override
{
cout << "Manager salary raised with incentives" << endl;
}
void promote() override
{
cout << "Manager promoted to Senior Manager" << endl;
}
};
// Derived class: Engineer
class Engineer : public Employee
{
public:
void raiseSalary() override
{
cout << "Engineer salary raised with bonus" << endl;
}
void promote() override
{
cout << "Engineer promoted to Senior Engineer" << endl;
}
};
int main()
{
// Create different employees
Manager m;
Engineer e;
// Array of base class pointers
Employee *employees[2] = {&m, &e};
// Raise salary for all employees
cout << "--- Raising Salaries ---" << endl;
for (int i = 0; i < 2; i++)
{
employees[i]->raiseSalary();
}
// Promote all employees
cout << "\n--- Promotions ---" << endl;
for (int i = 0; i < 2; i++)
{
employees[i]->promote();
}
return 0;
}