-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimePolymorphism.cpp
More file actions
57 lines (46 loc) · 1.36 KB
/
RuntimePolymorphism.cpp
File metadata and controls
57 lines (46 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
// Topic: Run-time Polymorphism
// Standard: C++20
// Build: g++ -std=c++20 -o runtime_poly RuntimePolymorphism.cpp
#include <iostream>
#include <memory>
#include <string>
class PaymentMethod {
public:
virtual ~PaymentMethod() = default;
virtual void pay(double amount) const = 0;
};
class CreditCard : public PaymentMethod {
public:
void pay(double amount) const override {
std::cout << "Paid " << amount << " using Credit Card\n";
}
};
class UPI : public PaymentMethod {
public:
void pay(double amount) const override {
std::cout << "Paid " << amount << " using UPI\n";
}
};
class Cash : public PaymentMethod {
public:
void pay(double amount) const override {
std::cout << "Paid " << amount << " using Cash\n";
}
};
void checkout(const PaymentMethod& method, double amount) {
method.pay(amount); // runtime decision based on actual object type
}
int main() {
std::cout << "=== Run-time Polymorphism ===\n\n";
CreditCard card;
UPI upi;
Cash cash;
checkout(card, 1200.50);
checkout(upi, 499.99);
checkout(cash, 200.00);
std::cout << "\nInterview Notes:\n";
std::cout << "1) Requires virtual function in base class\n";
std::cout << "2) Uses base reference/pointer for generic client code\n";
std::cout << "3) Method call resolved at runtime (dynamic dispatch)\n";
return 0;
}