You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users
// simply make the class private to acheive this.
// the only way to modify these classes is to use the get and set methods
//Example
classEmployee {
private:
// Private attribute
int salary;
public:
// Setter
voidsetSalary(int s) {
salary = s;
}
// Getter
intgetSalary() {
return salary;
}
};
intmain() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return0;
}
//Why we need encapsulation
/*
- It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts