Types of inheritance in C++ - Time & Space Complexity
When we use inheritance in C++, the program runs some code to create objects and connect classes. We want to see how the time it takes changes when we add more classes or objects.
How does the program's running time grow as we use different types of inheritance?
Analyze the time complexity of the following code snippet.
class Base {
public:
void baseMethod() {}
};
class Derived : public Base {
public:
void derivedMethod() {}
};
int main() {
Derived obj;
obj.baseMethod();
obj.derivedMethod();
return 0;
}
This code shows simple single inheritance where one class inherits from another and calls methods from both.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or recursion here; the program just creates one object and calls methods once.
- How many times: Each method is called exactly once.
Explain the growth pattern intuitively.
| Input Size (number of objects) | Approx. Operations |
|---|---|
| 1 | Few operations (create one object, call methods once) |
| 10 | About 10 times more operations (create 10 objects, call methods 10 times) |
| 100 | About 100 times more operations |
Pattern observation: The time grows linearly with the number of objects created and methods called.
Time Complexity: O(n)
This means if you create and use more objects, the time to run grows in a straight line with how many you have.
[X] Wrong: "Inheritance makes the program run slower exponentially because of all the parent classes."
[OK] Correct: Inheritance itself just connects classes; calling methods on objects grows linearly with how many objects you use, not exponentially.
Understanding how inheritance affects running time helps you explain your code clearly and shows you know how programs behave as they grow. This skill is useful when designing clean and efficient programs.
"What if we added a loop inside the derived class method that runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand inheritance types
Single inheritance means one class inherits from exactly one base class.Step 2: Match definition to options
Only Single inheritance describes a single derived class inheriting from one base class.Final Answer:
Single inheritance -> Option DQuick Check:
One derived, one base = Single inheritance [OK]
- Confusing multiple inheritance with single inheritance
- Thinking multilevel means one base class only
- Mixing hierarchical with single inheritance
Solution
Step 1: Recall multiple inheritance syntax
In C++, multiple inheritance uses commas to separate base classes with access specifiers.Step 2: Check each option
class Derived : public Base1, public Base2 {}; uses correct syntax: 'class Derived : public Base1, public Base2 {};'. Others have syntax errors.Final Answer:
class Derived : public Base1, public Base2 {}; -> Option BQuick Check:
Comma separates base classes with access specifiers [OK]
- Omitting commas between base classes
- Using '&' instead of commas
- Writing 'inherits' keyword which doesn't exist in C++
class A { public: void show() { std::cout << "A"; } };
class B : public A { public: void show() { std::cout << "B"; } };
class C : public B {};
int main() {
C obj;
obj.show();
return 0;
}Solution
Step 1: Understand inheritance chain
Class C inherits from B, which inherits from A. B overrides show() method.Step 2: Determine which show() is called
Calling obj.show() on C calls B's show() because C inherits B's version and does not override it.Final Answer:
B -> Option AQuick Check:
Derived class method called if overridden [OK]
- Assuming base class method runs instead of overridden
- Expecting 'C' output though C has no show()
- Thinking code causes compilation error
class Base { public: int x; };
class Derived : Base {};
int main() {
Derived d;
d.x = 1;
return 0;
}Solution
Step 1: Check inheritance syntax
In C++, inheritance requires an access specifier like public, private, or protected before base class name.Step 2: Identify the error
Derived : Base {} lacks access specifier; should be Derived : public Base {}.Final Answer:
Missing access specifier before Base in Derived class -> Option CQuick Check:
Inheritance needs access specifier [OK]
- Omitting access specifier in inheritance
- Thinking empty class body is error
- Confusing return type in main function
class Animal { public: void eat() { std::cout << "Eating"; } };
class Mammal : public Animal {};
class Bird : public Animal {};
class Bat : public Mammal, public Bird {};
int main() {
Bat b;
b.eat();
return 0;
}What problem arises and how to fix it?
Solution
Step 1: Analyze inheritance structure
Bat inherits Mammal and Bird, both inherit Animal, causing two Animal copies in Bat.Step 2: Identify ambiguity
Calling b.eat() is ambiguous because Bat has two Animal parts; compiler error occurs.Step 3: Fix with virtual inheritance
Use virtual inheritance for Mammal and Bird from Animal to share one Animal base.Final Answer:
Ambiguity error due to multiple inheritance; fix with virtual inheritance -> Option AQuick Check:
Diamond problem fixed by virtual inheritance [OK]
- Ignoring diamond problem ambiguity
- Thinking code compiles without error
- Assuming method override fixes ambiguity
Ready to prove it?
Pass the test and this pattern moves unknown
