Implementing C++ Inheritance

Learn how we can implement inheritance in C++, allowing our classes to share functionality
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_Animation_Style_vampire_beautiful_woman_blood_dark_evil_mal_0.jpg
Ryan McCombe
Ryan McCombe
Posted

In this lesson, we will see how we can start to implement inheritance in C++.

Lets imagine we have an Actor class, defined as follows:

class Actor {
  // Actor class code
};

We want to create a new class, Character, that will inherit from the Actor class.

We can do that in the following way:

class Character : Actor {
  // Character class code
};

After the name of the class we are creating, we can choose which class it inherits from. We do that using a : followed by the name of the class we want to be the parent.

Test your Knowledge

How can we create a Dog class that inherits from an Animal class?

C++ Class Inheritance Trees

In the previous lesson, we had this diagram for an inheritance tree we wanted to create:

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

Lets set it up in code:

class Actor {
public:
  void Render() {};
};

class Character : Actor {
public:
  void Move() {};
  void Attack() {};
  void DropLoot() {};
};

class Goblin : Character {
public:
  void Enrage() {};
};

class Dragon : Character {
public:
  void Fly() {};
};

C++ Public and Private Inheritance

Our code, at this point, might not work entirely as we expect. Lets try this:

Character PlayerOne;
PlayerOne.Render();

We will get an error claiming that Render is private. This may be surprising, as we have declared it as public within the Actor class.

However, just like functions and variables, inheritance itself can be public or private. By default, class inheritance is private.

We can change this to be public within our class heading:

class Character : public Actor {};

Now, anything that is public in Actor will be public in Monster

Setting the inheritance to public will not overrule access restrictions within the base class. If something was private in Actor, it will still be private in Monster.

In practice, public inheritance is by far the most useful and most commonly used, so it will be what we focus on in this course.

Test your Knowledge

What would be the effect of compiling and running the following code?

class Animal {
public:
  void Move() {};
};

class Dog : Animal {};

int main() {
  Dog MyDog;
  MyDog.Move();
}

How could we update the Dog class to fix this problem?

class Animal {
public:
  void Move() {};
}

class Dog : Animal {};

int main() {
  Dog MyDog;
  MyDog.Move();
}

C++ final - Preventing Inheritance

Sometimes, we create a class that is not designed to ever have a child class. To clarify that intent, and have the compiler to enforce it, we can add the final specifier to the class heading:

class Actor final {};

class Monster : Actor {};

Should anyone try to inherit from our class, as done on line 3 in the above example, their code will not compile. This will yield an error similar to the following:

Cannot inherit from 'Actor' as it has been declared as 'final'

Was this lesson useful?

Edit History

  • Fixed grammar issue in one of the quiz questions

  • First Published

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

C++ Protected Class Members

Learn about protected members within classes, and how they differ from public and private members.
3D art showing a dragon character
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved