Repository navigation
Expand file tree
/
Copy pathpolymorphism.cpp
More file actions
102 lines (86 loc) · 4.34 KB
/
Copy pathpolymorphism.cpp
File metadata and controls
102 lines (86 loc) · 4.34 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Polymorphism: one interface, many behaviours.
// - Compile time: function overloading picks a function by its parameter types.
// - Run time: virtual functions pick the implementation of the object's real
// type, even when the object is used through a base-class pointer or reference.
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
// ---------- Compile-time polymorphism: overloading ----------
class Formatter {
public:
static std::string format(int value) { return "int(" + std::to_string(value) + ")"; }
static std::string format(double value) { return "double(" + std::to_string(value) + ")"; }
static std::string format(const std::string& value) { return "string(\"" + value + "\")"; }
};
// ---------- Run-time polymorphism: virtual functions ----------
class Employee {
protected:
std::string name;
double baseSalary;
public:
Employee(std::string name, double baseSalary) : name(std::move(name)), baseSalary(baseSalary) {}
virtual ~Employee() = default; // a polymorphic base class needs a virtual destructor
virtual double monthlyPay() const { return baseSalary; }
virtual std::string role() const { return "Staff"; }
// NOT virtual: chosen at compile time from the type of the pointer/reference
std::string category() const { return "general"; }
const std::string& getName() const { return name; }
};
class Manager : public Employee {
double bonus;
public:
Manager(std::string name, double baseSalary, double bonus) : Employee(std::move(name), baseSalary), bonus(bonus) {}
double monthlyPay() const override { return baseSalary + bonus; } // override checks the signature
std::string role() const override { return "Manager"; }
std::string category() const { return "management"; } // hides Employee::category, does not override it
void approveBudget() const { std::cout << " " << name << " approved the quarterly budget\n"; }
};
class SalesPerson : public Employee {
double sales, commissionRate;
public:
SalesPerson(std::string name, double baseSalary, double sales, double commissionRate)
: Employee(std::move(name), baseSalary), sales(sales), commissionRate(commissionRate) {}
double monthlyPay() const override { return baseSalary + sales * commissionRate; }
std::string role() const override { return "Sales"; }
};
class Intern final : public Employee { // final: nothing can derive from Intern
public:
Intern(std::string name, double stipend) : Employee(std::move(name), stipend) {}
std::string role() const override { return "Intern"; }
// monthlyPay() is inherited unchanged
};
int main() {
std::cout << "Compile-time polymorphism (overloading):\n";
std::cout << " " << Formatter::format(42) << '\n';
std::cout << " " << Formatter::format(3.5) << '\n';
std::cout << " " << Formatter::format(std::string("OOP")) << "\n\n";
std::vector<std::unique_ptr<Employee>> staff;
staff.push_back(std::make_unique<Manager>("Ahmed", 250000, 60000));
staff.push_back(std::make_unique<SalesPerson>("Hina", 120000, 900000, 0.05));
staff.push_back(std::make_unique<Employee>("Omar", 150000));
staff.push_back(std::make_unique<Intern>("Noor", 40000));
std::cout << "Run-time polymorphism (same call, different behaviour):\n";
std::cout << std::fixed << std::setprecision(0);
double payroll = 0;
for (const auto& e : staff) {
double pay = e->monthlyPay(); // which version runs is decided at run time
std::cout << " " << std::left << std::setw(8) << e->getName() << std::setw(10) << e->role() << std::right
<< std::setw(10) << pay << '\n';
payroll += pay;
}
std::cout << " Total payroll: " << payroll << "\n\n";
std::cout << "Static vs dynamic binding:\n";
Manager m("Sana", 200000, 50000);
Employee& asEmployee = m;
std::cout << " asEmployee.role() = " << asEmployee.role() << " (virtual: Manager's version)\n";
std::cout << " asEmployee.category() = " << asEmployee.category() << " (non-virtual: Employee's version)\n";
std::cout << " m.category() = " << m.category() << "\n\n";
std::cout << "dynamic_cast safely finds the managers:\n";
for (const auto& e : staff) {
if (auto* manager = dynamic_cast<Manager*>(e.get())) manager->approveBudget();
}
return 0;
}