-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation.cpp
More file actions
68 lines (54 loc) Β· 1.55 KB
/
Encapsulation.cpp
File metadata and controls
68 lines (54 loc) Β· 1.55 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
#include <iostream>
using namespace std;
class Car {
private:
// Private attributes (encapsulated)
string make;
string model;
int year;
public:
// Constructor to initialize the car
Car(string carMake, string carModel, int carYear) {
make = carMake;
model = carModel;
year = carYear;
}
// Setter methods to set the values (Encapsulation)
void setMake(string carMake) {
make = carMake;
}
void setModel(string carModel) {
model = carModel;
}
void setYear(int carYear) {
year = carYear;
}
// Getter methods to access the values (Encapsulation)
string getMake() {
return make;
}
string getModel() {
return model;
}
int getYear() {
return year;
}
// Method to display car information with emojis
void displayInfo() {
cout << "π Car Information: " << getYear() << " " << getMake() << " " << getModel() << " ποΈ" << endl;
}
};
int main() {
// Creating an object of the Car class
Car myCar("Toyota", "Corolla", 2020);
// Accessing the object's methods to display info with emojis
myCar.displayInfo(); // Output: π Car Information: 2020 Toyota Corolla ποΈ
// Modifying the car details using setter methods
myCar.setMake("Honda");
myCar.setModel("Civic");
myCar.setYear(2023);
// Displaying updated car information
cout << "Updated Info: " << endl;
myCar.displayInfo(); // Output: π Car Information: 2023 Honda Civic ποΈ
return 0;
}