Control who can see and use which data members and methods. The primary tool for data hiding.
Inheritance access table
| Base class | Derived: private mode | Derived: protected mode | Derived: public mode |
|---|---|---|---|
| private | Not inherited | Not inherited | Not inherited |
| protected | → private | → protected | → protected |
| public | → private | → protected | → public |
Getters and Setters
class Student {
private:
int age;
public:
void setAge(int a) { // setter — validates before setting
if(a > 0) age = a;
}
int getAge() { // getter — read-only access
return age;
}
};