Topic 44 of 91 8. Object-Oriented Programming in C++ Interactive Tutorial

Access Control in C++: Public, Private, and Protected Members

8 mins read Last updated: 26 Sep 2026 20,528 views

Direct Answer & Core Concept

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.

Detailed Explanation & Mechanics

1. Visibility Matrix:

Architecture Diagram
┌───────────┬────────────────┬─────────────────┬─────────────────┐
│ 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.

Runnable Code Example: Access Control in C++

C++20 / C++23
main.cpp
Run Online
#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;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

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

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.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• 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.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.