C++ Hierarchical Inheritance
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Changing the base class without rechecking that every derived class still works.
- Using a base pointer to call a function only defined in a derived class, which does not compile.
- Forgetting
virtualon the base function, so derived versions are not called through base pointers.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: