Learn To Code In C++ For Unreal Engine – Inheritance

Table of Contents

Learn To Code In C++ For Unreal Engine – Inheritance

Reading Time: 2 minutes
Level: Beginner
Version: Visual Studio 2019 Community Edition

Help Others Learn Game Development

Inheritance goes hand in hand with classes and provides a way to extend the functionality of a class.

This way we can create different behaviours for different parts of our game all while enabling our code to share a set of attributes and functionalities.

What Is Inheritance?

Inheritance is a mechanism where you can derive a class from another class inheriting all the properties and functionalities of the inherited class and extending them with new implementations.

How can we declare inheritance or how do we denote that?

To explain inheritance better, let’s define a Player class:

				
					class Player
{

private:
	int Health;
	int Power;

public:
	void SetHealth(int NewHealth);
	int GetHealth();

	void SetPower(int NewPower);
	int GetPower();

	Player();
	Player(int InitialHealth, int InitialPower);

	void Attack();
	void Heal();
};
				
			

And inside the .cpp file for the Player class we are going to implement the code we declared above:

				
					#include "Player.h"
#include <iostream>

void Player::SetHealth(int NewHealth)
{
	Health = NewHealth;
}

int Player::GetHealth()
{
	return Health;
}

void Player::SetPower(int NewPower)
{
	Power = NewPower;
}

int Player::GetPower()
{
	return Power;
}

Player::Player()
{
}

Player::Player(int InitialHealth, int InitialPower)
{
	Health = InitialHealth;
	Power = InitialPower;
}

void Player::Attack()
{
	// implementation for this function
	std::cout << "Player is attacking";
}

void Player::Heal()
{
	// implementation for this function
	std::cout << "Player is healing";
}
				
			

We created a Player class and provided some properties and functionality for the class. 

Now, this class, is created for a generic player character who can attack and heal, and has power and health properties. Let us now inherit this class and create a more specific game character. 

On the project name right below the solution, Right Click -> Add -> Class. Give the class a name Warrior and click ok to create a new class.

To denote that the Warrior class inherits from the Player class, inside the Warrior.h file we need to do the following:

				
					#include "Player.h"

class Warrior : public Player
{
};
				
			

On line 1 we need to include the Player.h file to be able to inherit the Player class. On line 3 we add : public Player. 

This line of code will make the Warrior class inherit the Player class. We include the public keyword so that we will be able to access the public variables and functions from the Player class.

Now we can go inside the main function and create an object out of the Warrior class:

				
					#include "Warrior.h"

int main()
{
    Warrior w = Warrior();
    w.Attack();
}
				
			

If you run this app, you will see this printed in the console:

Img 1

First of all, the Attack function is not defined in the Warrior class, so how can we see the print above in the console?

The Attack function is defined inside the Player class, and the print we see is the line of code in that Attack function. 

Now that we inherited the Player class inside the Warrior class, we can use all public variables and functions defined in the Player class, and the Attack function is one of them.

This is the power of inheritance. 

We can have a common behavior modeled in the parent class(the class that is inherited) which will be the same for every child class(the class that inherits), and inside the child class we can model specific behavior for that class.

For example the warrior character can have a special attack where he throws swords at his enemies, we can define that function inside the Warrior.h file:

				
					#include "Player.h"

class Warrior : public Player
{

public:
	void ThrowSword();

};
				
			

And we can implement the function inside the Warrior.cpp file:

				
					#include "Warrior.h"
#include <iostream>

void Warrior::ThrowSword()
{
	std::cout << "The Warrior is throwing a sword";
}
				
			

Now we can call this function inside the main:

				
					#include "Warrior.h"

int main()
{
    Warrior w = Warrior();
    w.ThrowSword();
}
				
			

When you run the app, this is what you will see:

Img 2

Of course, we can model more behaviours that are specific for the warrior character, we can also add new variables and so on.

And we can do this for any class that inherits the Player class, there is no limit for this type of implementation.

Overriding Functions

So we can model new behaviors inside the child classes, but if we want to adapt an already existing behaviour to the child class?

For example, the attack functionality is something that every character will be able to do, but it will be different for every character. If we have a knight, archer and a king, they all attack differently.

But the current Attack function inherited from the Player class is a generic one, it has an attack specific to the generic Player class.

How can we modify the Attack function to be a specific function suited for the child class?

To modify a parent class you must have the same function signature in child class as well. By signature I mean the data type and sequence of parameters.

In our case the Attack function doesn’t have any parameters so we are going to declare it like this inside the Warrior.h:

				
					#include "Player.h"

class Warrior : public Player
{

public:
	void ThrowSword();
	void Attack();
};
				
			

Inside the Warrior.cpp we are going to implement the Attack function:

				
					void Warrior::Attack()
{
	std::cout << "The Warrior is attacking";
}
				
			

And now we can call the function inside the main:

				
					#include "Warrior.h"

int main()
{
    Warrior w = Warrior();
    w.Attack();
}
				
			

When we run the app this is what we will see in the console:

Img 3


Pointers, References And Objects

Everything we talked about in the lectures about pointers and references apply to objects as well.

Objects are also variables that are created from a specific class, and they have the type of the class from which they are created.

And since objects are variables we can use pointers to point to their memory spot, and we can use references to reference those variables.

For example:

				
					#include "Warrior.h"

int main()
{
    Warrior BraveWarrior = Warrior();
    
    Warrior* WarriorPointer = &BraveWarrior;
    
    Warrior& WarriorReference = BraveWarrior;
}
				
			

There is a small difference between the pointer and a reference to an object in terms of accessing its properties.

So far we use the dot( . ) and then we call the function name or a variable name from the class to access it, when it comes to reference that stays the same, but when it comes to pointers, instead of using dot ( . ) we use ” -> “:

				
					// to access the functions and properties use ->
WarriorPointer->Attack();

// to access the functions and properties use .
WarriorReference.Attack();

				
			

If we run this app, both lines of code will print the same statement in the console. The only difference is how we accessed one over the other.

One thing to note is that the object itself is a reference, so when we create:

				
					Warrior BraveWarrior = Warrior();
				
			

The BraveWarrior variable is a reference to the Warrior object in the memory, and it acts like a reference same as when we create a reference with the ” & ” character.

2 thoughts on “Learn To Code In C++ For Unreal Engine – Inheritance”

  1. I would like you to include the override, virtual, final words in the inheritance topic to make it fuller.

    sorry. My English is not so good

    Reply

Leave a Comment