C++ Inheritance - Why its Useful

An introduction to the problem inheritance is designed to help us solve, and how we can use it to manage future complexity.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art depicting a battle scene
Ryan McCombe
Ryan McCombe
Posted

When we start implementing more complex systems using classes, we quickly encounter a problem. We'll have multiple types of objects that have a lot of similar requirements. However, they're not so similar that we'd want them to both share the exact same class.

For example, lets imagine a future where we are trying to create three types of objects: rocks, goblins and dragons.

The Rocks just need one ability:

  • Be rendered to the screen

The Goblins need these abilities:

  • Be rendered to the screen
  • Move
  • Attack
  • DropLoot
  • Enrage

The Dragons need these abilities:

  • Be rendered to the screen
  • Move
  • Attack
  • DropLoot
  • Fly

Trying to model these as a single class is problematic. The class would be very complex. Additionally, most of our objects would be carrying around a lot of variables they will never use.

The ability to move would require variables to control things like movement speed. Having every rock carry around those variable seems like a waste of memory.

Equally, trying to create three totally separate classes for these objects isn't ideal. We'd end up having our render code repeated in 3 classes, and that will only get worse as we add more.

Fortunately, this situation is exactly what inheritance is designed to solve.

The Use Case for Inheritance (C++)

Inheritance allows to organise our classes into hierarchies. This allows a class to inherit the functions and variables of its parent. For example, lets create a class that we could use to create our rock objects. We'll call this the Actor class.

An actor is a common name for any simple class of things that can exist in our imagined world. It is where we will define the Render function:

A UML diagram showing the Actor class

Next, lets create a class for our Goblins. Goblins also need the ability to be rendered, but with inheritance, we no longer need to recreate that functionality. Actor already has that ability.

Goblins are just a more specific type of Actor, so we can have our Goblin class inherit all the abilities of the Actor class.

A UML diagram showing the Actor and Goblin classes

Now, our Goblin class has the Render ability, without needing to write any additional code. It just inherits it from the parent class.

Parents, Children, Subclasses and Base Classes

Hierarchical structures like these are very common in programming. There are many different terms used to describe the position of something within the hierarchy.

Phrases associated with family relationships are often used, such as parent, child, ancestor, descendant and more. For example:

  • Actor is a parent or an ancestor of Goblin
  • Goblin is a child or a descendant of Actor

Other popular terms include _sub class, base class and derived class. For example:

  • Goblin is a sub class of Actor
  • Goblin derives from Actor
  • Actor is the base class of Goblin

This tree structure provides our classes with a powerful ability. We get to have our dedicated, specific classes, but they don't need to duplicate the more generic, shared functions. They can instead just inherit those functions and variables from their ancestors.

Test your Knowledge

What is inheritance?

Using Class Inheritance in C++

In our above example, lets imagine we create an object from our Goblin class.

Goblin Bonker;

Bonker is a Goblin, ie, it is an instance of the Goblin class, therefore, it has access to the Attack() function, as we've seen before.

The cool thing with inheritance is that, because the Goblin class is a child of the Actor class, all Goblin objects are also Actor objects. This means that Goblins have access to all the functions and variables of the Actor class, too.

Goblin Bonker;

// Available because Bonker is an Actor
Bonker.Render();

// Available because Bonker is a Goblin
Bonker.Attack();

So, with this class hierarchy, all Goblin objects are inherently also Actor objects. The opposite is not necessarily true - an Actor is not necessarily also a Goblin. It could just be a plain Actor, or it could be some other subclass of actor.

Actor BigRock;

// Available because BigRock is an Actor
BigRock.Render();

// But we can't do this, because BigRock is not a Goblin
BigRock.Attack();

Test your Knowledge

If we have a Dog class that inherits from the Animal class, and we create a new object of type Dog, which of the following is true?

If we have a Dog class that inherits from the Animal class, and we try to create a new object using the Animal class, which of the following is true?

Multiple Layers of Inheritance

We haven't yet implemented our original design yet. Lets add the Dragon class, using the same approach:

A UML diagram showing the Actor, Goblin and Dragon classes

Something should seem off here. The Dragon and Goblin classes both have the Move, Attack and DropLoot functions. That's too much duplicate code.

Thankfully, with our new knowledge of inheritance, we might realise a way we can improve this design.

We shouldn't move that code into the Actor class, because simple objects like rocks don't need it. Instead, we can move that shared code into a new class. We can have both Goblin and Dragon inherit from it. Lets call this new class Character

A UML diagram showing the Actor, Character, Goblin and Dragon classes

Now, we have multiple layers of inheritance. With this setup, any Dragon we create will have the abilities of three different types:

Dragon Dave;

// Dave is an Actor
Dave.Render();

// And a Character
Dave.Attack();

// And a Dragon 
Dave.Fly();

That's enough theory for now - lets see how we can unlock the possibilities of inheritance.

We'll set up the above hierarchy in our code in the next lesson!

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Inheritance
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

Implementing C++ Inheritance

Learn how we can implement inheritance in C++, allowing our classes to share functionality
3D_Animation_Style_vampire_beautiful_woman_blood_dark_evil_mal_0.jpg
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved