C++ Programming 8. Object-Oriented Programming in C++ 8 mins read 20,534 views

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...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

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...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How do public, private, and protected access specifiers control visibility in classes and inheritance hierarchies?
Public members are accessible from any code outside the class. Private members are accessible only by member functions and friend entities of the class. Protected members are accessible within the class and by derived classes, but inaccessible to arbitrary outside code.

Explanation & In-Depth Details

1. Visibility Matrix:
```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

Q: Can a derived class access private members of its base class?
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

• Keep all member data private by default to uphold encapsulation.
• 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.
Academic Standards & Verified Citations

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:

C++ Programming