0
0
C++programming~10 mins

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

Choose your learning style9 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++.