In the previous chapter, we introduced some of the core concepts of C++. We started implementing variables and functions that could be used by characters within our game.
It's time to take a step back and talk about the wider context of what we're trying to achieve. Our variables and functions have, so far, not been connected to our characters in any structured way.
We've maybe hinted at the connection with the names, such as HeroHealth
and MonsterHealth
. But, to reach our goals, we need something much more robust.
Our game might have 100s of characters - we don't want to manage 100s of different variables. Our current approach is also quite limiting, as we need to know how many characters we need at the time we write our code.
This locks us off from doing things like spawning in additional monsters the longer our game is running.
In this lesson, we'll introduce the key concept that will unlock a world of possibilities. Classes are the key building block of object oriented programming.
Previously, we talked about different types of data in C++. This included things like booleans and integers. We also hinted about custom types of objects in our game - things like Monsters and Weapons
// Just like we can currently create integers and booleans
int SomeNumber;
bool SomeBoolean;
// We will also be able to create Goblins
Goblin Harry;
Classes are the main way we define these custom types.
The english definition of class is: a category of things having some property or attribute in common
It means exactly the same thing in programming. A class defines a set of properties and functions that are common to all objects that class.
For example, int
is a class. Objects of the int
class include things like 5
, 73
and 51261
.
The int
class gives its members certain abilities. For example, all of these numbers:
++
operator to increment themselves*
operator to multiply themselves togetherfloat
Similarly, string
is a class, that confers a different set of abilities to its objects.
In the next lesson, we will see how we can create our own class, for things that are specific to our software.
Deciding what classes we might need to create is largely about looking at the objects that need to exist in our game, and coming up with sensible groupings.
For example, our game might have two Goblins that our Hero needs to fight. In other words, two Goblin objects.
These two objects will have a lot in common - they are, after all, both Goblins. They are both likely to have variables such as Health
, AttackDamage
and Level
.
They will also want to share the same functions - things like TakeDamage()
, DropLoot()
and many more.
Therefore, we can think of these two Goblin objects as being members of the same class.
What is a Class?
There is often some confusion at this point about what differentiates an Object from a Class. This will become clear as we start creating classes and objects in future lessons, but lets discuss it briefly here.
The numbers 1
and 2
are specific integers - they are objects. On the other hand, int
is a class. It is a description of what integers are, and what they can do.
The integer
class is where functions and operators are defined. The int
class also confers the ability to construct new int
objects. We've been making use of this ability every time we create an integer variable:
int One { 1 };
int Two { 2 };
Because of this property, people often conceptualise classes as blueprints when explaining the concept. The class is a blueprint for creating a specific type of object. Many objects can be created from the same blueprint.
Similarly, in the below example, John
and Megan
are specific Goblins - they are objects.
Goblin John;
Goblin Megan;
The Goblin
class is not a specific Goblin - it is an description of what Goblins are, and the blueprints to create more Goblin objects.
Now that we have decided we need to group our goblins under a class, we need to decide what that class actually is, and what to call it. So far, we've been imagining a Goblin
class, but that is perhaps too specific.
We will have other types of monsters too, and those monsters are likely to have very similar properties and functions. For example, all monsters will be able to move and attack - not just Goblins.
Perhaps we should be slightly less specific, and instead create the Monster
class?
The class of Monster
is more generic than a Goblin - it could embody the notion of what it is to be a Goblin, a Demon, a Dragon, and any other type of monster.
This process of generalisation, where we take specific objects and group them into more general categories is called abstraction
What is Abstraction?
Abstraction is one of the main principles of object oriented design.
Future chapters will talk about about the other principles. This will include tools we have to create more powerful designs.
For example, we will soon see ways that our Goblin objects can be members of both the Goblin
class and Monster
class at once, inheriting the abilities of both.
For now, though, lets just create our first class - the Monster
class. We'll see that in the next lesson!
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way