0
0
C++programming~10 mins

Types of inheritance in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Types of inheritance
Base Class
Single Inheritance
Base Class
Derived1
Multiple Inheritance
Base1
Derived
Base
Derived1
Derived2
Base
Derived1
Derived2
Derived3
Shows how classes inherit from one or more base classes in different ways: single, multiple, multilevel, hierarchical, and hybrid inheritance.
Execution Sample
C++
class Base {
public:
  int x = 10;
};

class Derived : public Base {
public:
  int y = 20;
};
Defines a base class with a variable and a derived class that inherits it and adds another variable.
Execution Table
StepActionClassVariable AccessedValueExplanation
1Create Base objectBasex10Base class object has x=10
2Create Derived objectDerivedx10Derived inherits x from Base
3Access Derived.yDerivedy20Derived class own variable y=20
4Access Derived.xDerivedx10Derived accesses inherited x=10
5End---Execution ends
💡 All variables accessed and inheritance verified, program ends.
Variable Tracker
VariableBase ObjectDerived Object
x1010
y-20
Key Moments - 3 Insights
Why can Derived access variable x even though it is declared in Base?
Because Derived inherits from Base, it gets all public members like x. See execution_table row 4 where Derived.x is 10.
What happens if we create an object of Base and try to access y?
Base does not have y, so accessing y on Base object is invalid. See execution_table row 1 where only x exists.
How does multiple inheritance differ from single inheritance?
Multiple inheritance means a class inherits from more than one base class, combining their members. Single inheritance inherits from only one base class.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x in the Derived object at step 4?
A10
B20
C0
DUndefined
💡 Hint
Check execution_table row 4 where Derived.x is accessed.
At which step does the Derived object get its own variable y initialized?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 where Derived.y is accessed.
If we add another base class and inherit from both, what type of inheritance is this?
ASingle inheritance
BMultiple inheritance
CMultilevel inheritance
DHierarchical inheritance
💡 Hint
Refer to concept_flow diagram showing multiple inheritance with two base classes.
Concept Snapshot
Types of inheritance in C++:
- Single: One base, one derived.
- Multiple: One derived, multiple bases.
- Multilevel: Chain of inheritance.
- Hierarchical: One base, many derived.
- Hybrid: Combination of above.
Derived classes inherit members from base classes.
Full Transcript
This visual execution shows different types of inheritance in C++. We start with a base class having a variable x. A derived class inherits from it and adds variable y. The execution table traces creating objects and accessing variables, showing how inheritance works. Key moments clarify why derived classes can access base members and differences between inheritance types. The quiz tests understanding of variable values and inheritance types. The concept snapshot summarizes the main inheritance types and their behavior.