← Back to C++ Course | Chapter 9: Inheritance | Lesson 5 of 8

C++ Multilevel Inheritance

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

cpp
#include <iostream>

class Grandparent {
public:
	void greet() { std::cout << "Grandparent" << std::endl; }
};

class Parent : public Grandparent {};
class Child : public Parent {};

int main() {
	Child c;
	c.greet();
	return 0;
}

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

cpp
#include <iostream>

class Grandparent {
public:
	Grandparent() { std::cout << "Grandparent" << std::endl; }
};

class Parent : public Grandparent {
public:
	Parent() { std::cout << "Parent" << std::endl; }
};

class Child : public Parent {
public:
	Child() { std::cout << "Child" << std::endl; }
};

int main() {
	Child c;
	return 0;
}

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

cpp
#include <iostream>

class Grandparent {
public:
	int value;
	Grandparent(int v) : value(v) {}
};

class Parent : public Grandparent {
public:
	Parent(int v) : Grandparent(v) {}
};

class Child : public Parent {
public:
	Child(int v) : Parent(v) {}
};

int main() {
	Child c(42);
	std::cout << c.value << std::endl;
	return 0;
}

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

cpp
#include <iostream>

class Grandparent {
public:
	int value = 100;
};

class Parent : public Grandparent {};
class Child : public Parent {};

int main() {
	Child c;
	std::cout << c.value << std::endl;
	return 0;
}

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

cpp
#include <iostream>

class Employee {
public:
	void work() { std::cout << "Working" << std::endl; }
};

class Manager : public Employee {
public:
	void manage() { std::cout << "Managing" << std::endl; }
};

class SeniorManager : public Manager {
public:
	void strategize() { std::cout << "Strategizing" << std::endl; }
};

int main() {
	SeniorManager sm;
	sm.work();
	sm.manage();
	sm.strategize();
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.