Access Control in C++: Public, Private, and Protected Members
What is Access Control in C++: Public, Private, and Protected Members? Public members are accessible from any code outside the class. Private members are accessible only by member functions and friend entities of the class. Protect...
How do public, private, and protected access specifiers control visibility in classes and inheritance hierarchies?
Explanation & In-Depth Details
```text
┌───────────┬────────────────┬─────────────────┬─────────────────┐
│ Specifier │ Same Class │ Derived Class │ Outside World │
├───────────┼────────────────┼─────────────────┼─────────────────┤
│ public │ Accessible │ Accessible │ Accessible │
│ protected │ Accessible │ Accessible │ Inaccessible │
│ private │ Accessible │ Inaccessible │ Inaccessible │
└───────────┴────────────────┴─────────────────┴─────────────────┘
```
2. Inheritance Access Modes:
- `class D : public B`: Public remains public, protected remains protected.
- `class D : protected B`: Public and protected both become protected in D.
- `class D : private B`: All base members become private in D.
Code Example
#include <iostream>
#include <string>
class Employee {
private:
double salary; // Hidden completely from outside and derived classes
protected:
std::string internalDepartment; // Accessible to derived subclasses
public:
std::string name; // Accessible to everyone
Employee(std::string n, std::string dept, double sal)
: salary(sal), internalDepartment(std::move(dept)), name(std::move(n)) {}
double getSalary() const { return salary; } // Controlled public getter
};
class Manager : public Employee {
public:
Manager(std::string n, double sal) : Employee(std::move(n), "Management", sal) {}
void showDetails() const {
// Can access 'name' (public) and 'internalDepartment' (protected)
std::cout << "Manager: " << name << " | Department: " << internalDepartment
<< " | Salary: $" << getSalary() << "\n";
}
};
int main() {
Manager mgr("Alice Vance", 95000.0);
mgr.showDetails();
std::cout << "Public name: " << mgr.name << "\n";
// mgr.internalDepartment -> ERROR (Protected outside)
// mgr.salary -> ERROR (Private)
return 0;
}Interview Question & Tips
A: No. Derived classes can only access public and protected members of their base class. Private members remain strictly encapsulated inside the base class.
Q: When should you use protected inheritance (`class Derived : protected Base`)?
A: Protected inheritance is rarely used; it is appropriate when you want to reuse implementation details of Base across a hierarchy without exposing Base interface to users.
Best Practices
• Use protected members sparingly; favor accessor methods in the base class over direct protected variable access.
• Use public inheritance for standard "is-a" polymorphic relationships.
This study reference for Access Control in C++: Public, Private, and Protected Members is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: