-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.cpp
More file actions
99 lines (81 loc) · 1.89 KB
/
car.cpp
File metadata and controls
99 lines (81 loc) · 1.89 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
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
int year;
string make;
string model;
int speed = 0;
public:
void setYear(int);
void setMake(string);
void setModel(string);
void setSpeed(int);
int getYear();
string getMake();
string getModel();
int getSpeed();
void accelerate();
void brake();
};
void Car::setYear(int x) {
year = x;
}
int Car::getYear() {
return year;
}
void Car::setMake(string y) {
make = y;
}
string Car::getMake() {
return make;
}
void Car::setModel(string z) {
model = z;
}
string Car::getModel() {
return model;
}
void Car::setSpeed(int spd) {
speed = spd;
}
int Car::getSpeed() {
return speed;
}
void Car::accelerate() {
speed += 5;
}
void Car::brake() {
if (speed > 5) {
speed -= 5;
} else {
speed = 0;
}
}
int main() {
Car myCar;
int Year = 0;
string Make, Model;
cout << "Please enter the year of the vehicle: ";
cin >> Year;
cout << "Please enter the make of the vehicle: ";
cin >> Make;
cout << "Please enter the model of the vehicle: ";
cin >> Model;
myCar.setYear(Year);
cout << "You entered the year of the car as " << myCar.getYear() << endl;
myCar.setMake(Make);
cout << "You entered the make of the car as " << myCar.getMake() << endl;
myCar.setModel(Model);
cout << "You entered the model of the car as " << myCar.getModel() << endl;
for (int i = 0; i < 5; ++i) {
myCar.accelerate();
cout << "Accelerating. The current speed of the car is: " << myCar.getSpeed() << endl;
}
for (int j = 0; j < 5; ++j) {
myCar.brake();
cout << "Decelerating. The current speed of the car is: " << myCar.getSpeed() << endl;
}
return 0;
}