The gatekeepers of your code. Master public, private, and protected.
Think of a class like a building. Different areas have different rules about who can enter.
Accessible from anywhere. Like a park, anyone can walk in.
Accessible ONLY within the class. Strictly confidential.
Accessible in the class AND by children (derived classes).
See how the compiler reacts in real-time. Change the access specifier of the goldBars variable inside the BankVault class and observe if main() can access it.
The variable is public. This means anyone, including the external main() function, can directly read and write to it. It's like leaving the vault door wide open.
class BankVault {
public::
int goldBars = 100;
};
int main() {
BankVault myVault;
// Trying to access from outside
myVault.goldBars = 50;
return 0;
}
When a class inherits from another class, the access levels can change based on the type of inheritance.
1. Which specifier allows members to be accessed by derived classes but NOT by the outside world?
2. In 'class Car : private Vehicle', how do public members of Vehicle appear inside Car?