Access control in inheritance in C++ - Time & Space Complexity
When we use inheritance in C++, access control decides which parts of a base class are visible in a derived class.
We want to see how this visibility check affects the time it takes to access members as the program grows.
Analyze the time complexity of accessing members with different inheritance access specifiers.
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class Derived : public Base {
public:
void access() {
int a = x; // public member
int b = y; // protected member
// int c = z; // private member, not accessible
}
};
This code shows how public, protected, and private members are accessed in inheritance.
In this example, there are no loops or recursion, but member access happens repeatedly in larger programs.
- Primary operation: Accessing class members through inheritance.
- How many times: Each access is a single operation, but many accesses can happen as program size grows.
Accessing members is a direct operation and does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 member accesses |
| 100 | 100 member accesses |
| 1000 | 1000 member accesses |
Pattern observation: Each member access takes the same time regardless of how many members or classes exist.
Time Complexity: O(1)
This means accessing a member through inheritance takes constant time, no matter how big the program is.
[X] Wrong: "Accessing protected or public members in inheritance takes longer as the class grows."
[OK] Correct: Member access is direct and does not depend on class size or inheritance depth for time cost.
Understanding that access control does not slow down member access helps you focus on design, not performance worries.
"What if we added virtual inheritance or virtual functions? How would that affect the time complexity of member access?"
Practice
Derived inherits from a class Base using public inheritance, what happens to the public members of Base in Derived?Solution
Step 1: Understand public inheritance effect
In public inheritance, the access levels of the base class members remain the same in the derived class.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.Final Answer:
They remain public in Derived. -> Option BQuick Check:
Public members stay public with public inheritance [OK]
- Thinking public members become protected or private
- Confusing inheritance types
- Assuming private members become accessible
Derived that inherits privately from a class Base in C++?Solution
Step 1: Recall private inheritance syntax
Private inheritance is declared using the keywordprivatebefore the base class name.Step 2: Match syntax options
class Derived : private Base {}; usesprivatecorrectly:class Derived : private Base {};Final Answer:
class Derived : private Base {}; -> Option AQuick Check:
Private inheritance uses 'private' keyword [OK]
- Omitting inheritance access specifier
- Using 'public' or 'protected' instead of 'private'
- Confusing default inheritance for classes
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?
Solution
Step 1: Analyze access inside Derived::func()
Private memberzof Base is not accessible in Derived, so Line A causes a compile error.Step 2: Analyze access from main()
Derived inherits Baseprotectedly, so Base's publicxbecomes protected in Derived. Hence,d.xis inaccessible outside Derived, so Line B causes a compile error.Final Answer:
Line A causes a compile error; Line B causes a compile error. -> Option AQuick Check:
Private hidden; protected inheritance hides public as protected [OK]
- Assuming private members accessible in derived
- Thinking public members stay public with protected inheritance
- Ignoring access from outside derived class
class Base {
private:
int secret;
public:
Base() : secret(42) {}
};
class Derived : public Base {
public:
void reveal() {
std::cout << secret << std::endl;
}
};Solution
Step 1: Identify access error
Membersecretis private in Base, so Derived cannot access it directly inreveal().Step 2: Fix access by changing access specifier
Changingsecrettoprotectedallows Derived to access it directly.Final Answer:
Error: 'secret' is private in Base; fix by making 'secret' protected. -> Option DQuick Check:
Private members hidden; protected allows derived access [OK]
- Trying to access private members directly
- Using friend function incorrectly
- Changing inheritance type unnecessarily
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?Solution
Step 1: Understand access changes with inheritance types
Public inheritance keepsapublic, so it remains accessible outside. Private inheritance hidesabut also hidesbfrom further derived classes.Step 2: Choose inheritance that hides
Protected inheritance makes public members protected (hidingabut keepsbaccessible in Derived and subclassesafrom outside) and keeps protected members protected (allowing access tobin Derived and subclasses).Final Answer:
Protected inheritance -> Option CQuick Check:
Protected inheritance hides public, keeps protected accessible [OK]
- Choosing public inheritance which keeps public members public
- Choosing private inheritance which hides protected members too
- Thinking manual copying is needed
Ready to prove it?
Pass the test and this pattern moves unknown
