-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstraction.cpp
More file actions
115 lines (98 loc) · 3.72 KB
/
Copy pathAbstraction.cpp
File metadata and controls
115 lines (98 loc) · 3.72 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
#include <iostream>
#include <string>
using namespace std;
/*
Abstract class -->
1. Act as an interface for the outside world to operate the car.
2. This abstract class tells 'WHAT' all it can do rather then 'HOW' it does that.
3. Since this is an abstract class we cannot directly create Objects of this class.
4. We need to Inherit it first and then that child class will have the responsibility to
provide implementation details of all the abstract (virtual) methods in the class.
5. In our real world example of Car, imagine you sitting in the car and able to operate
the car (startEngine, accelerate, brake, turn) just by pressing or moving some
pedals/buttons/ steer the wheel etc. You dont need to know how these things work, and
also they are hidden under the hood.
6. This Class 'Car' denotes that (pedals/buttons/steering wheel etc).
*/
class Car {
public:
//virtual sirf ye bta rha hai hum isko bss declare kr rhe hai define nhi kr rhe hai
//-->define krne ki jimaddari child class ke pass hoga jo isko inherit karega
virtual void startEngine() = 0;
virtual void shiftGear(int gear) = 0;
virtual void accelerate() = 0;
virtual void brake() = 0;
virtual void stopEngine() = 0;
virtual ~Car() {}
};
/*
1. This is a Concrete class (A class that provide implementation details of an abstract class).
Now anyone can make an Object of 'SportsCar' and can assign it to 'Car' (Parent class) pointer
(See main method for this)
2. In our real world example of Car, as you cannot have a real car by just having its body only
(all these buttons or steering wheel). You need to have the actual implementation of 'What' happens
when we press these buttons. 'SportsCar' class denotes that actual implementation.
3. Therefore, to denote a real world car in programming we created 2 classes.
One to denote all the user-interface like pedals, buttons, steering wheels etc ('Car' class).And,
Another one to denote the actual car with all the implementations of these buttons (SportsCar' class).
*/
class SportsCar : public Car {
public:
string brand;
string model;
bool isEngineOn;
int currentSpeed;
int currentGear;
SportsCar(string b, string m) {
this->brand = b;
this->model = m;
isEngineOn = false;
currentSpeed = 0;
currentGear = 0;
}
void startEngine() {
isEngineOn = true;
cout << brand << " " << model << " : Engine starts with a roar!" << endl;
}
void shiftGear(int gear) {
if (!isEngineOn) {
cout << brand << " " << model << " : Engine is off! Cannot Shift Gear." << endl;
return;
}
currentGear = gear;
cout << brand << " " << model << " : Shifted to gear " << currentGear << endl;
}
void accelerate() {
if (!isEngineOn) {
cout << brand << " " << model << " : Engine is off! Cannot accelerate." << 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;
}
void stopEngine() {
isEngineOn = false;
currentGear = 0;
currentSpeed = 0;
cout << brand << " " << model << " : Engine turned off." << endl;
}
};
// Main Method
int main() {
//passing brand and model
Car* myCar = new SportsCar("Ford", "Mustang");
myCar->startEngine();
myCar->shiftGear(1);
myCar->accelerate();
myCar->shiftGear(2);
myCar->accelerate();
myCar->brake();
myCar->stopEngine();
delete myCar;
return 0;
}