-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-37.cpp
96 lines (72 loc) · 2.35 KB
/
Day-37.cpp
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
// Ques 1. You are building a Car class in C++ that simulates a simple car system. The class should encapsulate the following attributes:
// brand (string): The car's brand name.
// model (string): The car's model name.
// year (int): The manufacturing year of the car.
// speed (int): The current speed of the car.
// The class should provide:
// A constructor to initialize the brand, model, and year.
// Getters for all attributes.
// Setters for speed only (you should not allow external modification of the brand, model, or year).
// A method to increaseSpeed(int amount) that increases the current speed by a given amount.
// A method to decreaseSpeed(int amount) that decreases the current speed, but the speed should never go below 0.
// Task:
// Implement the Car class with proper encapsulation.
// Create a main function where you create an object of the Car class, modify its speed using the provided methods, and print the car's details after each speed change.
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
string brand;
string model;
int year;
int speed;
public:
// Constructor
Car(string b, string m, int y) : brand(b), model(m), year(y), speed(0) {}
// Getters
string getBrand() const {
return brand;
}
string getModel() const {
return model;
}
int getYear() const {
return year;
}
int getSpeed() const {
return speed;
}
// Setter for speed
void setSpeed(int s) {
speed = s;
}
// Method to increase speed
void increaseSpeed(int amount) {
speed += amount;
}
// Method to decrease speed
void decreaseSpeed(int amount) {
speed = (speed - amount < 0) ? 0 : speed - amount;
}
// Method to print car details
void printDetails() const {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << ", Speed: " << speed << " km/h" << endl;
}
};
int main() {
// Create a Car object
Car myCar("Toyota", "Corolla", 2021);
// Print initial details
myCar.printDetails();
// Increase speed
myCar.increaseSpeed(30);
myCar.printDetails();
// Decrease speed
myCar.decreaseSpeed(10);
myCar.printDetails();
// Attempt to decrease speed below 0
myCar.decreaseSpeed(25);
myCar.printDetails();
return 0;
}