-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstaticAndDynamicPolymorphism.cpp
More file actions
167 lines (139 loc) · 5.01 KB
/
Copy pathstaticAndDynamicPolymorphism.cpp
File metadata and controls
167 lines (139 loc) · 5.01 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
using namespace std;
/*
Program 1 demonstrates both compile-time and run-time polymorphism. Compile-time polymorphism is achieved through method overloading (accelerate() and accelerate(int)), while run-time polymorphism is achieved using virtual functions and method overriding in ManualCar and ElectricCar, accessed through a Car* base pointer.
*/
// Base Car class
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 accelerate(int speed) = 0; //Abstract method for Static 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 and overloading accelerate at the same time.
void accelerate(int speed) {
if (!isEngineOn) {
cout << brand << " " << model << " : Cannot accelerate! Engine is off." << endl;
return;
}
currentSpeed += speed;
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 accelerate - Dynamic Polymorphism
void accelerate(int speed) {
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 + speed;
currentSpeed += speed;
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("Ford", "Mustang");
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();
//remove
delete myManualCar;
delete myElectricCar;
return 0;
}