-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation_OOPs.cpp
More file actions
47 lines (36 loc) · 1.01 KB
/
Encapsulation_OOPs.cpp
File metadata and controls
47 lines (36 loc) · 1.01 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
#include <iostream>
#include <cstring>
using namespace std;
// Class definition
class Car {
private:
// Private members are not directly accessible outside the class
string brand;
int year;
public:
// Public members can be accessed from outside the class
// Constructor with parameters
Car(string b, int y) : brand(b), year(y) {}
// Public member functions to access private members
void setBrand(string b) {
brand = b;
}
string getBrand() const {
return brand;
}
void setYear(int y) {
year = y;
}
int getYear() const {
return year;
}
};
int main() {
// Create an object of the Car class
Car myCar("Toyota", 2022);
// Access and modify object's properties using public member functions
cout << "Brand: " << myCar.getBrand() << ", Year: " << myCar.getYear() << endl;
myCar.setYear(2023);
cout << "Updated Year: " << myCar.getYear() << endl;
return 0;
}