C++ Hierarchical Inheritance
In this page:
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 <iostream>
class Shape {
public:
void describe() { std::cout << "I am a shape" << std::endl; }
};
class Circle : public Shape {};
class Square : public Shape {};
int main() {
Circle c;
Square s;
c.describe();
s.describe();
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 <iostream>
class Shape {
public:
int sides = 0;
};
class Circle : public Shape {};
class Square : public Shape {};
int main() {
Circle c;
Square s;
s.sides = 4;
std::cout << c.sides << " " << s.sides << std::endl;
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 <iostream>
class Shape {};
class Circle : public Shape {
public:
void roll() { std::cout << "Rolling" << std::endl; }
};
class Square : public Shape {
public:
void stack() { std::cout << "Stacking" << std::endl; }
};
int main() {
Circle c;
Square s;
c.roll();
s.stack();
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 <iostream>
class Shape {
public:
virtual void describe() { std::cout << "Shape" << std::endl; }
};
class Circle : public Shape {
public:
void describe() override { std::cout << "Circle" << std::endl; }
};
int main() {
Shape *ptr = new Circle();
ptr->describe();
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 <iostream>
class Shape {
public:
Shape() { std::cout << "Shape created" << std::endl; }
};
class Circle : public Shape {
public:
Circle() { std::cout << "Circle created" << std::endl; }
};
int main() {
Circle c;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: