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

C++ Hierarchical Inheritance

Hierarchical inheritance is when one parent has several children, like siblings who all inherit from the same mom. Each child can then be different in its own way.
Syntax
cpp
class Parent { };

class Child1 : public Parent { };
class Child2 : public Parent { };

What is Hierarchical Inheritance?

Hierarchical inheritance is a structure where multiple distinct child classes all inherit from a single shared parent class, forming a tree with one root and several branches.

It's the natural fit whenever several related types share common behavior but also diverge from each other.

Example: What is Hierarchical Inheritance?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	void describe() { std::cout << "I am a shape" << std::endl; }
};

// Define the type Circle
class Circle : public Shape {};
class Square : public Shape {};

// Program execution starts in main()
int main() {
	Circle c;
	Square s;
	c.describe();
	s.describe();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Shared Base Members

Every child class in this structure shares the variables and methods defined in the parent, but each object still gets its own independent copy of that inherited data — a Circle and a Square both inheriting a shared color member don't affect each other's color.

Example: Shared Base Members

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	int sides = 0;
};

// Define the type Circle
class Circle : public Shape {};
class Square : public Shape {};

// Program execution starts in main()
int main() {
	Circle c;
	Square s;
	s.sides = 4;
	// Print to the console with cout
	std::cout << c.sides << " " << s.sides << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Unique Child Behavior

Even though every child shares the same parent, each is free to define methods of its own that no sibling class has, which is exactly what differentiates them from one another despite their common ancestry.

Example: Unique Child Behavior

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {};

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	void roll() { std::cout << "Rolling" << std::endl; }
};

class Square : public Shape {
// Members below can be accessed from outside the class
public:
	void stack() { std::cout << "Stacking" << std::endl; }
};

// Program execution starts in main()
int main() {
	Circle c;
	Square s;
	c.roll();
	s.stack();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Class Pointers

A pointer typed as the parent class can point to an object of any of its child classes, which makes it easy to store a mixed collection of related objects in a single container and process them uniformly through their shared base interface.

Example: Base Class Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	virtual void describe() { std::cout << "Shape" << std::endl; }
};

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	void describe() override { std::cout << "Circle" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Shape *ptr = new Circle();
	ptr->describe();
	// Free the heap memory with delete
	delete ptr;
	return 0;
}

Constructor Chaining

When a child object is created, the parent's constructor runs first to establish the shared base state, and only then does the child's own constructor run to configure whatever makes that particular subclass unique.

Example: Constructor Chaining

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	Shape() { std::cout << "Shape created" << std::endl; }
};

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	Circle() { std::cout << "Circle created" << std::endl; }
};

// Program execution starts in main()
int main() {
	Circle c;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Changing the base class without rechecking that every derived class still works.
  2. Using a base pointer to call a function only defined in a derived class, which does not compile.
  3. Forgetting virtual on the base function, so derived versions are not called through base pointers.

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.