-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInheritence.cpp
More file actions
124 lines (101 loc) · 3.47 KB
/
Copy pathInheritence.cpp
File metadata and controls
124 lines (101 loc) · 3.47 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
#include <iostream>
#include <string>
using namespace std;
/*
We know that real world Objects show inheritance relationship where we
have parent object and child object. child object have all the characters
or behaviours that parent have plus some additional characters/behaviours.
Like all cars in real world have a brand, model etc and can start, stop,
accelerate etc. But some specific cars like manual car have gear System
while other specific cars like Electric cars have battery system.
We represent this scenario of real world in programming by creating a parent class and
defining all the characters(variables) or behaviours(methods) that all cars
have in parent class. Then we create different child classes that inherits
from this parent class and define only those characters and behaviours
that are specific to them. Although objects of these child classes can
access or call parent class characters(variables) and behaviours(methods).
Hence providing code reusability.
*/
class Car {
protected:
string brand;
string model;
bool isEngineOn;
int currentSpeed;
public:
Car(string b, string m) {
this->brand = b;
this->model = m;
isEngineOn = false;
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;
}
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;
}
void brake() {
currentSpeed -= 20;
if (currentSpeed < 0) currentSpeed = 0;
cout << brand << " " << model << " : Braking! Speed is now " << currentSpeed << " km/h" << endl;
}
virtual ~Car() {}
};
class ManualCar : public Car { // Inherits from Car
private:
int currentGear; //spcific to Manual Car.
public:
ManualCar(string b, string m) : Car(b, m) {
currentGear = 0;
}
//Specialized method for Manual Car
void shiftGear(int gear) {
currentGear = gear;
cout << brand << " " << model << " : Shifted to gear " << currentGear << endl;
}
};
class ElectricCar : public Car { // Inherits from Car
private:
int batteryLevel; //spcific to Electric Car.
public:
ElectricCar(string b, string m) : Car(b, m) {
batteryLevel = 100;
}
//specialized method for Electric Car
void chargeBattery() {
batteryLevel = 100;
cout << brand << " " << model << " : Battery fully charged!" << endl;
}
};
// Main Method
int main() {
ManualCar* myManualCar = new ManualCar("Suzuki", "WagonR");
myManualCar->startEngine();
myManualCar->shiftGear(1); //specific to manual car
myManualCar->accelerate();
myManualCar->brake();
myManualCar->stopEngine();
delete myManualCar;
cout << "----------------------" << endl;
ElectricCar* myElectricCar = new ElectricCar("Tesla", "Model S");
myElectricCar->chargeBattery(); //specific to electric car
myElectricCar->startEngine();
myElectricCar->accelerate();
myElectricCar->brake();
myElectricCar->stopEngine();
delete myElectricCar;
return 0;
}