0
0
C++programming~15 mins

Access control in inheritance in C++ - Deep Dive

Choose your learning style9 modes available
Overview - Access control in inheritance
What is it?
Access control in inheritance is about how the properties and functions of a parent class can be used or hidden in a child class. It controls whether the child class can see or change the parent's members. This helps organize code and protect important parts from being changed accidentally. In C++, access control uses keywords like public, protected, and private to set these rules.
Why it matters
Without access control in inheritance, all parts of a parent class would be open to any child class, which can cause mistakes or security problems. It would be like giving someone the keys to your whole house when they only need to enter one room. Access control helps keep code safe, clear, and easier to maintain by limiting what child classes can access.
Where it fits
Before learning access control in inheritance, you should understand basic classes, objects, and simple inheritance in C++. After this, you can learn about polymorphism and virtual functions, which build on how inheritance works with access control.
Mental Model
Core Idea
Access control in inheritance decides which parts of a parent class a child class can use or see, like setting different levels of privacy for family members.
Think of it like...
Imagine a family house where some rooms are open to all family members, some rooms are only for close relatives, and some rooms are locked for parents only. Access control in inheritance works like these room permissions for child classes.
Parent Class Members
┌───────────────┐
│ public        │ <─ Accessible everywhere
│ protected     │ <─ Accessible in child classes
│ private       │ <─ Accessible only in parent class
└───────────────┘

Inheritance Types:
  public inheritance: public → public, protected → protected, private → inaccessible
  protected inheritance: public → protected, protected → protected, private → inaccessible
  private inheritance: public → private, protected → private, private → inaccessible
Build-Up - 7 Steps
1
FoundationUnderstanding class members and access
🤔
Concept: Learn what public, protected, and private mean for class members.
In C++, class members can be public, protected, or private. - Public members can be accessed from anywhere. - Protected members can be accessed only inside the class and its child classes. - Private members can be accessed only inside the class itself. Example: class Parent { public: int a; // accessible everywhere protected: int b; // accessible in Parent and children private: int c; // accessible only in Parent };
Result
Members have different visibility levels controlling who can use them.
Understanding member access is the foundation for controlling inheritance behavior.
2
FoundationBasic inheritance and member access
🤔
Concept: See how child classes inherit members and how access affects visibility.
When a class inherits from a parent, it gets the parent's members. - Public members remain public in the child. - Protected members remain protected. - Private members are not accessible directly in the child. Example: class Child : public Parent { void func() { a = 1; // OK, public b = 2; // OK, protected // c = 3; // Error, private } };
Result
Child class can use public and protected members but not private ones.
Knowing which members are inherited and accessible helps avoid errors.
3
IntermediateEffect of inheritance types on access
🤔Before reading on: Do you think private inheritance keeps parent public members public in the child? Commit to your answer.
Concept: Inheritance type (public, protected, private) changes how parent members appear in the child.
C++ has three inheritance types: - Public inheritance: keeps public and protected members as they are. - Protected inheritance: public and protected members become protected in the child. - Private inheritance: public and protected members become private in the child. Example: class Child : private Parent { void func() { a = 1; // OK inside Child } }; Outside Child, a is not accessible through Child objects.
Result
Inheritance type controls how accessible parent members are through the child class.
Inheritance type is a powerful tool to control access beyond member keywords.
4
IntermediateAccess control and object usage
🤔Before reading on: Can you access protected members of a parent class through a child class object? Commit to your answer.
Concept: Access control affects what members can be accessed through objects of the child class.
Even if a child class can access protected members inside its code, those members are not accessible through child objects outside the class. Example: Child obj; obj.a = 5; // OK if a is public obj.b = 5; // Error if b is protected Inside Child methods, b can be used, but outside, it cannot.
Result
Protected members are hidden from outside code using child objects.
Understanding object access prevents common mistakes when using inherited members.
5
IntermediateOverriding access with using declarations
🤔Before reading on: Can a child class make a protected parent member public? Commit to your answer.
Concept: Child classes can change access of inherited members using 'using' declarations.
A child class can promote or demote access of inherited members. Example: class Child : protected Parent { public: using Parent::a; // makes 'a' public in Child }; Now, 'a' is accessible through Child objects even though inheritance is protected.
Result
Access control can be customized in the child class for inherited members.
Knowing how to adjust access helps design flexible and safe class hierarchies.
6
AdvancedPrivate members and inheritance limits
🤔Before reading on: Are private members inherited by child classes? Commit to your answer.
Concept: Private members exist in the parent but are not accessible or visible in child classes directly.
Private members are part of the parent class's memory but cannot be accessed or overridden by children. Example: class Parent { private: int secret; }; class Child : public Parent { void func() { // secret = 5; // Error: private member } }; Child objects still have 'secret' but cannot use it directly.
Result
Private members are inherited but hidden, protecting internal details.
Understanding private inheritance limits helps avoid design mistakes and bugs.
7
ExpertAccess control and multiple inheritance surprises
🤔Before reading on: Does access control behave the same with multiple inheritance? Commit to your answer.
Concept: Multiple inheritance can cause complex access control interactions and ambiguities.
When a class inherits from multiple parents, access control rules apply separately to each. Conflicts can arise if parents have members with the same name but different access. Example: class A { protected: int x; }; class B { public: int x; }; class C : public A, public B { void func() { // x = 1; // Error: ambiguous which x A::x = 1; // OK B::x = 1; // OK } };
Result
Multiple inheritance requires careful access control management to avoid ambiguity.
Knowing these subtleties prevents hard-to-find bugs in complex class hierarchies.
Under the Hood
C++ compilers implement access control by checking member access at compile time. The access keywords (public, protected, private) are metadata that restrict code visibility. Inheritance types modify how these access levels are mapped in the child class's interface. Private members are stored in the object's memory but are inaccessible to child class code, enforced by compiler rules. Access control does not affect memory layout but controls what code can legally use which members.
Why designed this way?
Access control was designed to protect encapsulation, a core object-oriented principle. It prevents accidental misuse of internal details and enforces clear interfaces. The three levels provide flexibility for different design needs. Inheritance types allow controlling how much of the parent's interface is exposed by the child, supporting different design patterns like 'is-a' or 'implemented-in-terms-of'. This design balances safety, flexibility, and performance.
Inheritance Access Control Flow

Parent Class Members
┌───────────────┐
│ public        │
│ protected     │
│ private       │
└───────────────┘
        │
        ▼
Inheritance Type
┌───────────────┐
│ public        │
│ protected     │
│ private       │
└───────────────┘
        │
        ▼
Child Class Members
┌───────────────┐
│ public or     │
│ protected or  │
│ private       │
│ (depends on   │
│ inheritance)  │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Can private members be accessed by child classes? Commit to yes or no.
Common Belief:Private members are inherited and accessible in child classes just like protected members.
Tap to reveal reality
Reality:Private members are inherited but cannot be accessed or used directly by child classes.
Why it matters:Assuming private members are accessible leads to compile errors and confusion about class design.
Quick: Does private inheritance mean the child class's public members become private? Commit to yes or no.
Common Belief:Private inheritance only hides private members from outside, public members stay public in the child.
Tap to reveal reality
Reality:Private inheritance makes all inherited public and protected members private in the child class.
Why it matters:Misunderstanding this causes unexpected access errors when using child class objects.
Quick: Can you access protected members of a parent class through a child class object? Commit to yes or no.
Common Belief:Protected members are accessible through child class objects just like public members.
Tap to reveal reality
Reality:Protected members are not accessible through child class objects outside the class; only inside child class code.
Why it matters:This misconception leads to access errors and confusion about object interfaces.
Quick: Does changing inheritance type affect the parent's private members? Commit to yes or no.
Common Belief:Inheritance type changes how private members are accessed in the child class.
Tap to reveal reality
Reality:Inheritance type does not affect private members; they remain inaccessible regardless of inheritance type.
Why it matters:Believing otherwise causes incorrect assumptions about class design and access.
Expert Zone
1
Access control affects compile-time visibility but does not change the memory layout of objects.
2
Using 'using' declarations to change access can break encapsulation if not done carefully.
3
Multiple inheritance can cause subtle access conflicts that require explicit disambiguation.
When NOT to use
Avoid private inheritance when you want to express an 'is-a' relationship; use public inheritance instead. For composition or 'has-a' relationships, prefer member objects over inheritance. When access control becomes too complex, consider redesigning class hierarchies to simplify.
Production Patterns
Public inheritance is used to model 'is-a' relationships exposing interfaces. Protected inheritance is rare but used to restrict interface exposure while allowing child classes access. Private inheritance is used to implement 'implemented-in-terms-of' relationships, hiding parent interfaces. Using 'using' declarations to adjust access is common in library design to expose selected members.
Connections
Encapsulation
Access control in inheritance is a direct extension of encapsulation principles.
Understanding access control deepens the grasp of how encapsulation protects data and behavior in object-oriented design.
Information Hiding in Security
Both restrict access to sensitive parts to prevent misuse or attacks.
Knowing access control in programming helps appreciate broader security concepts where hiding information reduces risk.
Role-based Access Control (RBAC)
Both assign different access levels based on roles or relationships.
Seeing access control in inheritance as a form of RBAC clarifies how permissions depend on context and hierarchy.
Common Pitfalls
#1Trying to access private parent members directly in child class code.
Wrong approach:class Child : public Parent { void func() { c = 5; // Error: 'c' is private in Parent } };
Correct approach:class Parent { protected: int c; }; class Child : public Parent { void func() { c = 5; // OK: 'c' is protected } };
Root cause:Misunderstanding that private members are inaccessible in child classes.
#2Using private inheritance when public inheritance is intended.
Wrong approach:class Child : private Parent { // Child hides Parent's public members }; Child obj; obj.a = 5; // Error: 'a' is private in Child
Correct approach:class Child : public Parent { // Child exposes Parent's public members }; Child obj; obj.a = 5; // OK
Root cause:Confusing inheritance types and their effect on access control.
#3Accessing protected members through child class objects outside the class.
Wrong approach:Child obj; obj.b = 10; // Error: 'b' is protected
Correct approach:class Child : public Parent { void func() { b = 10; // OK inside Child } };
Root cause:Not distinguishing between access inside class code and through objects.
Key Takeaways
Access control in inheritance uses public, protected, and private keywords to control visibility of parent members in child classes.
Inheritance types (public, protected, private) change how parent members are exposed through the child class interface.
Private members are inherited but hidden and cannot be accessed directly by child classes.
Protected members are accessible inside child classes but hidden from outside code using child objects.
Understanding access control prevents common errors and helps design clear, safe class hierarchies.