Bird
Raised Fist0
C++programming~10 mins

Access control in inheritance in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Access control in inheritance
Base class members
↓
Inheritance type?
↓
Derived class access
Shows how base class members' access changes in derived class depending on inheritance type.
Execution Sample
C++
class Base {
public:
  int x;
protected:
  int y;
private:
  int z;
};

class Derived : public Base {
  void func() {
    x = 1; y = 2; // z not accessible
  }
};
Defines base class with different access levels and derived class accessing members under public inheritance.
Execution Table
StepCode LineMember AccessAccess Allowed?Reason
1class Base { public: int x; }xPublicAccessible everywhere
2class Base { protected: int y; }yProtectedAccessible in Base and derived classes
3class Base { private: int z; }zPrivateAccessible only in Base
4class Derived : public BaseInheritance typePublicPublic inheritance keeps access levels
5Derived::func() { x = 1; }xAccessiblex is public in Base and remains public
6Derived::func() { y = 2; }yAccessibley is protected in Base and remains protected
7Derived::func() { z = 3; }zNot accessiblez is private in Base, not inherited
8class Derived : protected BaseInheritance typeProtectedPublic and protected become protected
9Derived::func() { x = 1; }xAccessiblex becomes protected in Derived
10Derived::func() { y = 2; }yAccessibley remains protected
11Derived::func() { z = 3; }zNot accessiblez is private in Base, not inherited
12class Derived : private BaseInheritance typePrivatePublic and protected become private
13Derived::func() { x = 1; }xAccessiblex becomes private in Derived
14Derived::func() { y = 2; }yAccessibley becomes private in Derived
15Derived::func() { z = 3; }zNot accessiblez is private in Base, not inherited
16Outside Derived: obj.xxDepends on inheritancePublic inheritance: accessible; protected/private: not accessible
17Outside Derived: obj.yyNot accessibleProtected and private members not accessible outside
18Outside Derived: obj.zzNot accessiblePrivate member never accessible outside Base
19End of example--Demonstrates access control changes in inheritance
πŸ’‘ All private members of Base are not accessible in Derived or outside; access levels of public/protected members depend on inheritance type.
Variable Tracker
MemberBase AccessPublic InheritanceProtected InheritancePrivate Inheritance
xpublicpublicprotectedprivate
yprotectedprotectedprotectedprivate
zprivatenot inheritednot inheritednot inherited
Key Moments - 3 Insights
Why can't Derived class access private member z of Base?
Private members are only accessible inside their own class. Execution table rows 3, 7, 11, and 15 show z is never accessible in Derived.
How does inheritance type affect access to public member x in Derived?
Public inheritance keeps x public (row 5), protected inheritance makes x protected (row 9), private inheritance makes x private (row 13). See variable_tracker for summary.
Can code outside Derived access protected member y?
No, protected members are not accessible outside the class or its derived classes. Execution table row 17 shows outside access denied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table row 5. What is the access level of member x inside Derived under public inheritance?
APrivate
BProtected
CPublic
DNot accessible
πŸ’‘ Hint
Check row 5 in execution_table where x is assigned inside Derived under public inheritance.
At which step does the code show that private member z is not accessible in Derived?
AStep 5
BStep 7
CStep 9
DStep 13
πŸ’‘ Hint
Look for 'z not accessible' in execution_table rows 7, 11, or 15.
If Derived inherits Base privately, what is the access level of member y inside Derived?
APrivate
BProtected
CPublic
DNot accessible
πŸ’‘ Hint
See variable_tracker row for y and column 'Private Inheritance'.
Concept Snapshot
Access control in inheritance:
- Base class members: public, protected, private
- Public inheritance: public->public, protected->protected
- Protected inheritance: public->protected, protected->protected
- Private inheritance: public->private, protected->private
- Private members never inherited
- Outside access depends on final access level
Full Transcript
This visual execution trace shows how access control works in C++ inheritance. The base class has members with public, protected, and private access. Depending on the inheritance type (public, protected, or private), the access level of inherited members changes in the derived class. Private members are never inherited and cannot be accessed in the derived class. Public inheritance keeps public members public and protected members protected. Protected inheritance changes public members to protected. Private inheritance changes public and protected members to private. Outside the derived class, only public members are accessible. The execution table traces each step of member access and inheritance type, while the variable tracker summarizes access changes. Key moments clarify common confusions about private member access and inheritance effects. The quiz tests understanding of access levels at different steps. This helps beginners see exactly how access control changes with inheritance in C++.

Practice

(1/5)
1. In C++, if a class Derived inherits from a class Base using public inheritance, what happens to the public members of Base in Derived?
easy
A. They are not accessible in Derived.
B. They remain public in Derived.
C. They become private in Derived.
D. They become protected in Derived.

Solution

  1. Step 1: Understand public inheritance effect

    In public inheritance, the access levels of the base class members remain the same in the derived class.
  2. Step 2: Apply to public members

    Public members of the base class stay public in the derived class, so they can be accessed from outside through derived objects.
  3. Final Answer:

    They remain public in Derived. -> Option B
  4. Quick Check:

    Public members stay public with public inheritance [OK]
Hint: Public inheritance keeps public members public [OK]
Common Mistakes:
  • Thinking public members become protected or private
  • Confusing inheritance types
  • Assuming private members become accessible
2. Which of the following is the correct syntax for declaring a class Derived that inherits privately from a class Base in C++?
easy
A. class Derived : private Base {};
B. class Derived : public Base {};
C. class Derived : protected Base {};
D. class Derived private Base {};

Solution

  1. Step 1: Recall private inheritance syntax

    Private inheritance is declared using the keyword private before the base class name.
  2. Step 2: Match syntax options

    class Derived : private Base {}; uses private correctly: class Derived : private Base {};
  3. Final Answer:

    class Derived : private Base {}; -> Option A
  4. Quick Check:

    Private inheritance uses 'private' keyword [OK]
Hint: Use 'private' keyword for private inheritance [OK]
Common Mistakes:
  • Omitting inheritance access specifier
  • Using 'public' or 'protected' instead of 'private'
  • Confusing default inheritance for classes
3. Consider the following code:
class Base {
public:
    int x;
protected:
    int y;
private:
    int z;
};

class Derived : protected Base {
public:
    void func() {
        x = 1;
        y = 2;
        // z = 3; // Line A
    }
};

int main() {
    Derived d;
    // d.x = 5; // Line B
    return 0;
}

Which statements are true?
medium
A. Line A causes a compile error; Line B causes a compile error.
B. Line A is fine; Line B causes a compile error.
C. Line A causes a compile error; Line B is fine.
D. Both Line A and Line B are fine.

Solution

  1. Step 1: Analyze access inside Derived::func()

    Private member z of Base is not accessible in Derived, so Line A causes a compile error.
  2. Step 2: Analyze access from main()

    Derived inherits Base protectedly, so Base's public x becomes protected in Derived. Hence, d.x is inaccessible outside Derived, so Line B causes a compile error.
  3. Final Answer:

    Line A causes a compile error; Line B causes a compile error. -> Option A
  4. Quick Check:

    Private hidden; protected inheritance hides public as protected [OK]
Hint: Private members always inaccessible; protected inheritance hides public [OK]
Common Mistakes:
  • Assuming private members accessible in derived
  • Thinking public members stay public with protected inheritance
  • Ignoring access from outside derived class
4. Given the code below, identify the error and fix it:
class Base {
private:
    int secret;
public:
    Base() : secret(42) {}
};

class Derived : public Base {
public:
    void reveal() {
        std::cout << secret << std::endl;
    }
};
medium
A. No error; code works fine.
B. Error: 'secret' is private in Base; fix by inheriting Base privately.
C. Error: 'secret' is private in Base; fix by using virtual inheritance.
D. Error: 'secret' is private in Base; fix by making 'secret' protected.

Solution

  1. Step 1: Identify access error

    Member secret is private in Base, so Derived cannot access it directly in reveal().
  2. Step 2: Fix access by changing access specifier

    Changing secret to protected allows Derived to access it directly.
  3. Final Answer:

    Error: 'secret' is private in Base; fix by making 'secret' protected. -> Option D
  4. Quick Check:

    Private members hidden; protected allows derived access [OK]
Hint: Private members inaccessible; use protected for derived access [OK]
Common Mistakes:
  • Trying to access private members directly
  • Using friend function incorrectly
  • Changing inheritance type unnecessarily
5. You have a base class Base with a public member int a; and a protected member int b;. You want to create a derived class Derived that hides a from outside users but allows access to b inside Derived and its subclasses. Which inheritance type should you use?
hard
A. Public inheritance
B. Private inheritance
C. Protected inheritance
D. No inheritance; copy members manually

Solution

  1. Step 1: Understand access changes with inheritance types

    Public inheritance keeps a public, so it remains accessible outside. Private inheritance hides a but also hides b from further derived classes.
  2. Step 2: Choose inheritance that hides a but keeps b accessible in Derived and subclasses

    Protected inheritance makes public members protected (hiding a from outside) and keeps protected members protected (allowing access to b in Derived and subclasses).
  3. Final Answer:

    Protected inheritance -> Option C
  4. Quick Check:

    Protected inheritance hides public, keeps protected accessible [OK]
Hint: Protected inheritance hides public, keeps protected accessible [OK]
Common Mistakes:
  • Choosing public inheritance which keeps public members public
  • Choosing private inheritance which hides protected members too
  • Thinking manual copying is needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard