C++ Multilevel Inheritance
In this page:
class Grandparent { };
class Parent : public Grandparent { };
class Child : public Parent { };
Understanding Multilevel Inheritance
In multilevel inheritance, a derived class itself becomes the base class for yet another derived class, forming a chain — grandparent, parent, grandchild — rather than the simple one-level relationship of single inheritance.
Example: Understanding Multilevel Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Grandparent
class Grandparent {
// Members below can be accessed from outside the class
public:
void greet() { std::cout << "Grandparent" << std::endl; }
};
// Define the type Parent
class Parent : public Grandparent {};
class Child : public Parent {};
// Program execution starts in main()
int main() {
Child c;
c.greet();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Call Sequence
Constructors along this chain run strictly top-down: grandparent first, then parent, then grandchild, since each level depends on the level above it being fully initialized before it can build on top of it.
Destructors then run in the exact opposite order.
Example: Constructor Call Sequence
// Include std::cout and std::cin
#include <iostream>
// Define the type Grandparent
class Grandparent {
// Members below can be accessed from outside the class
public:
Grandparent() { std::cout << "Grandparent" << std::endl; }
};
// Define the type Parent
class Parent : public Grandparent {
// Members below can be accessed from outside the class
public:
Parent() { std::cout << "Parent" << std::endl; }
};
class Child : public Parent {
public:
Child() { std::cout << "Child" << std::endl; }
};
// Program execution starts in main()
int main() {
Child c;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameter Passing in Multilevel Chain
A grandchild class can forward initialization parameters up through the chain by passing them through each intermediate constructor's initializer list, letting a single call at the bottom configure state defined all the way up at the grandparent level.
Example: Parameter Passing in Multilevel Chain
// Include std::cout and std::cin
#include <iostream>
// Define the type Grandparent
class Grandparent {
// Members below can be accessed from outside the class
public:
int value;
Grandparent(int v) : value(v) {}
};
// Define the type Parent
class Parent : public Grandparent {
// Members below can be accessed from outside the class
public:
Parent(int v) : Grandparent(v) {}
};
class Child : public Parent {
public:
Child(int v) : Parent(v) {}
};
// Program execution starts in main()
int main() {
Child c(42);
// Print to the console with cout
std::cout << c.value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Member Access in Multilevel Inheritance
Members declared in the topmost class remain accessible all the way down in the bottommost class, as long as each link in the chain preserved their visibility as public or protected rather than narrowing it to private along the way.
Example: Member Access in Multilevel Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Grandparent
class Grandparent {
// Members below can be accessed from outside the class
public:
int value = 100;
};
// Define the type Parent
class Parent : public Grandparent {};
class Child : public Parent {};
// Program execution starts in main()
int main() {
Child c;
// Print to the console with cout
std::cout << c.value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Real-World Multilevel Scenarios
Multilevel inheritance naturally models things that already form a hierarchy in the real world, such as a general Employee class refined into a Manager class, which is then further refined into a SeniorManager class with its own additional responsibilities.
Example: Real-World Multilevel Scenarios
// Include std::cout and std::cin
#include <iostream>
// Define the type Employee
class Employee {
// Members below can be accessed from outside the class
public:
void work() { std::cout << "Working" << std::endl; }
};
// Define the type Manager
class Manager : public Employee {
// Members below can be accessed from outside the class
public:
void manage() { std::cout << "Managing" << std::endl; }
};
class SeniorManager : public Manager {
public:
void strategize() { std::cout << "Strategizing" << std::endl; }
};
// Program execution starts in main()
int main() {
SeniorManager sm;
sm.work();
sm.manage();
sm.strategize();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to pass constructor arguments up the chain.
- Assuming destructors run in the same order as constructors, when they run in reverse.
- Confusing multilevel with multiple inheritance.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: