One of the first things you'll hear when it comes to programming is the concept called called Object-Oriented Programming. This is a major feature of C++, and it is a commonly adopted pattern for building complex projects.
When designing object oriented programs, we think of our code in terms of objects. Objects are just a digital representation of something our software is trying to represent, or simulate. They're a way to organise our project into distinct entities.
If we were making banking software, we might be creating and managing objects that represent customers and currencies. For a library, it might be books and employees. For games, it could be dragons, weapons and magical spells.
A commonly recommended first step to identify what objects our software might have is to start with a plain language description of what we're building.
In Mythic Quest, our heroes enter dungeons and kill monsters to collect magical items
By picking out nouns from this description, we can identify some of the objects we might want to be creating. This might be things such as:
Just like objects in the real world, objects in programming have properties, and things that they can do.
In programming, we often call these object properties "variables", and the actions that the objects can perform are called "functions".
The term modeling is often used to describe the process of designing our digital objects. Modeling is the process of reducing a complex physical object or system into a simplified representation - a model. This model is simplified so that we can manipulate it with code.
However, a "model" is also the name given to a 3D art asset, and "modeling" is the activity of creating such assets.
Therefore, in the context of computer graphics where we are frequently using such 3D assets, the terminology is commonly reserved for that meaning instead.
We'll look at functions later in this chapter - for the next few lessons, we'll focus on variables.
Variables are pieces of data that help describe the current state of our object. Let’s take one of our monsters as an example. Some things we might need to keep track of for our monster are:
Answers to these questions would be stored as variables on each monster object.
We can notice from the list above that there are different types of data we might need to store.
Whether the monster is dead will only have two possible values - it is either true or it is false. For Health, we might expect that value to be a number. For the name, it might be a string of alphabetic characters.
To store information about what weapon the monster has equipped, our variable might need to store a another object entirely - a Weapon
object, with its own set of variables.
For now, the key point is to realise that variables can store different types of data.
C++ is what is known as a strongly typed language. This means that we, as developers, typically need to be explicit about the type of data that is being stored in each variable.
Some common data types include:
Data Type | Description | Examples |
---|---|---|
bool | A boolean - either true or false | true , false |
int | An integer - a whole number | 42 , 5 , -621 , 0 |
float | A decimal ("floating point") number | 3.4 , -2.71 , 4.0 |
string | A collection of characters strung together | "Hello World!" |
From the table above, only the first three of these data types - boolean
, int
and float
- are available to us out of the box.
We've seen we were able to use strings in the previous lesson. That worked because the code we included at the top of our file loaded in support for strings.
For now, if we ever find we're having issues where we can't seem to create strings, be sure we're using the code structure provided in the previous lesson:
#include <iostream>
using namespace std;
int main() {
}
Once we identify what type of data we want to store in our variable, we next need to give our variable a name - an identifier which we will use in our code.
There are some restrictions to these names. The most notabe restriction is that an identifier cannot contain a space, and the name cannot start with a number. But beyond those restrictions, we can mostly call our variables whatever we want.
However, it is strongly recommended that we choose sensible, descriptive names for our variables.
For example, if a variable is intended to store a character's current amount of health, a name like Health
would be much better than H
or x
.
There's no technical reason why we can't call our variable things like H
or a
. However, once our code base gets bigger, it can be very difficult to make sense of it if our variables do not have meaningful names.
When we want to use multiple words to describe our variable, there are some conventions to bypass the inability to use spaces. Common approaches include using underscores or capitalisation, for example: currentHealth
, CurrentHealth
or Current_Health
.
What would be a good name for a variable that stores whether or not a character is dead?
Let's revisit our requirements for our Monster objects, and map our variables up to a data type and a name.
Data Type | Variable Name | Description | Example |
---|---|---|---|
bool | isDead | Is the monster currently dead? | false |
int | Health | How much health the monster has remaining | 150 |
float | Armor | How much damage resistance the monster has | 0.2 |
string | Name | The monster's name | "Goblin Warrior" |
position | Location | Coordinates of where the monster is in the 3D world | x=45.2, y=152.5, z=90.1 |
weapon | Weapon | The monster's currently equipped weapon | Steel Axe |
The first four data types are things C++ can provide us with.
For things like a position
and a weapon
, those seem much more specific to our needs.
So, they're unlikely to be available to us be default - we'd need to create those data types ourselves. We'll see how to do that in Chapter 3.
For now, though, we will just be using booleans, integers, floats and strings, so we can establish the basics.
Let's explore how we can create variables in C++ in the next lesson!
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way