-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncapulation.cpp
More file actions
116 lines (95 loc) · 3.06 KB
/
Copy pathEncapulation.cpp
File metadata and controls
116 lines (95 loc) · 3.06 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
#include <iostream>
#include <string>
using namespace std;
/*
Encapsulation says 2 things:
1. An Object's Characteristics and its behaviour are encapsulated together
within that Object.
2. All the characteristics or behaviours are not for everyone to access.
Object should provide data security.
We follow above 2 pointers about Object of real world in programming by:
1. Creating a class that act as a blueprint for Object creation. Class contain
all the characteristics (class variable) and behaviour (class methods) in one block,
encapsulating it together.
2. We introduce access modifiers (public, private, protected) etc to provide data
security to the class members.
*/
class SportsCar {
private:
//characters
string brand;
string model;
bool isEngineOn;
int currentSpeed;
int currentGear;
//Introduce new variable to explain setters
string tyreCompany;
public:
SportsCar(string b, string m) {
this->brand = b;
this->model = m;
isEngineOn = false;
currentSpeed = 0;
currentGear = 0;
tyreCompany = "MRF";
}
//behaviour-->method
int getSpeed() {
return currentSpeed;
}
string getTyreCompany() {
return tyreCompany;
}
void setTyreCompany(string tyreCompany) {
this->tyreCompany = tyreCompany;
}
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;
}
~SportsCar() {}
};
// Main Method
int main() {
SportsCar* mySportsCar = new SportsCar("Ford", "Mustang");
mySportsCar->startEngine();
mySportsCar->shiftGear(1);
mySportsCar->accelerate();
mySportsCar->shiftGear(2);
mySportsCar->accelerate();
mySportsCar->brake();
mySportsCar->stopEngine();
// //Setting arbitrary value to speed.
// mySportsCar->currentSpeed = 500;
// cout << "Current Speed of My Sports Car is set to " << mySportsCar->currentSpeed << endl;
cout << "Current Speed of My Sports Car is " << mySportsCar->getSpeed() << endl;
delete mySportsCar;
return 0;
}