-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicPolymorphism.cpp
More file actions
144 lines (121 loc) · 4.23 KB
/
Copy pathDynamicPolymorphism.cpp
File metadata and controls
144 lines (121 loc) · 4.23 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <string>
using namespace std;
/*
Dynamic Polymorphism in real life says that 2 Objects coming from same
family will respond to same stimulus differently. Like in real world Manual
car and Electric car will respond to accelerate() differently.
To represent this in programming, we create a parent class that defines all
characters and behaviours that are generic to all child classes and are also same in
all child classes but make those methods abstract(virtual) that are generic to all
child classes but all child class will behave differently. Then those child class
will provide implementation details of these abstract methods the way they want.
*/
class Car {
protected:
string brand;
string model;
bool isEngineOn;
int currentSpeed;
public:
Car(string brand, string model) {
this->brand = brand;
this->model = model;
this->isEngineOn = false;
this->currentSpeed = 0;
}
//Common methods for All cars.
void startEngine() {
isEngineOn = true;
cout << brand << " " << model << " : Engine started." << endl;
}
void stopEngine() {
isEngineOn = false;
currentSpeed = 0;
cout << brand << " " << model << " : Engine turned off." << endl;
}
virtual void accelerate() = 0; // Abstract method for Dynamic Polymorphism
virtual void brake() = 0; // Abstract method for Dynamic Polymorphism
virtual ~Car() {} // Virtual destructor
};
class ManualCar : public Car {
private:
int currentGear;
public:
ManualCar(string brand, string model) : Car(brand, model) {
this->currentGear = 0;
}
//Specialized method for Manual Car
void shiftGear(int gear) {
currentGear = gear;
cout << brand << " " << model << " : Shifted to gear " << currentGear << endl;
}
// Overriding accelerate - Dynamic Polymorphism
void accelerate() {
if (!isEngineOn) {
cout << brand << " " << model << " : Cannot accelerate! Engine is off." << endl;
return;
}
currentSpeed += 20;
cout << brand << " " << model << " : Accelerating to " << currentSpeed << " km/h" << endl;
}
// Overriding brake - Dynamic Polymorphism
void brake() {
currentSpeed -= 20;
if (currentSpeed < 0) currentSpeed = 0;
cout << brand << " " << model << " : Braking! Speed is now " << currentSpeed << " km/h" << endl;
}
};
class ElectricCar : public Car {
private:
int batteryLevel;
public:
ElectricCar(string brand, string model) : Car(brand, model) {
this->batteryLevel = 100;
}
//specialized method for Electric Car
void chargeBattery() {
batteryLevel = 100;
cout << brand << " " << model << " : Battery fully charged!" << endl;
}
// Overriding accelerate - Dynamic Polymorphism
void accelerate() {
if (!isEngineOn) {
cout << brand << " " << model << " : Cannot accelerate! Engine is off." << endl;
return;
}
if (batteryLevel <= 0) {
cout << brand << " " << model << " : Battery dead! Cannot accelerate." << endl;
return;
}
batteryLevel -= 10;
currentSpeed += 15;
cout << brand << " " << model << " : Accelerating to " << currentSpeed << " km/h. Battery at " << batteryLevel << "%." << endl;
}
// Overriding brake - Dynamic Polymorphism
void brake() {
currentSpeed -= 15;
if (currentSpeed < 0) currentSpeed = 0;
cout << brand << " " << model << " : Regenerative braking! Speed is now " << currentSpeed << " km/h. Battery at " << batteryLevel << "%." << endl;
}
};
// Main function
int main() {
Car* myManualCar = new ManualCar("Suzuki", "WagonR");
myManualCar->startEngine();
myManualCar->accelerate();
myManualCar->accelerate();
myManualCar->brake();
myManualCar->stopEngine();
cout << "----------------------" << endl;
Car* myElectricCar = new ElectricCar("Tesla", "Model S");
myElectricCar->startEngine();
myElectricCar->accelerate();
myElectricCar->accelerate();
myElectricCar->brake();
myElectricCar->stopEngine();
// Cleanup
delete myManualCar;
delete myElectricCar;
return 0;
}