One of the first things you'll hear when it comes to programming is the concept 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 organize our project into distinct entities.
This approach not only simplifies the design of complex software but also makes it easier to maintain as the project gets more and more complex.
For instance, in object-oriented design, a game would treat each player, enemy, and weapon as distinct, interactible objects within our code. Aside from keeping our code organised, this modular structure also mirrors real-world interactions, making the design of our project more intuitive and aligned with how we naturally perceive the world.
A way to identify what objects our software might have is to start with a plain language description of what we're building. For example:
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 types of 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 actions that they can perform.
In programming, we often call these object properties variables, and the actions that the objects can perform are called functions.
For the next few lessons, we'll focus on variables. We'll look at functions later in the next chapter of the course.
The term modeling is often used to describe the process of identifying what variables and functions our digital objects need to have. Modeling is the process of reducing a complex physical object or system into a simplified representation - a model - that we can interact with using 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.
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. For example:
Weapon
object, with its own set of variables.For now, the key point is to realize that each variable stores data of a specific type.
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 |
char | An individual character | 'a', 'B', '@' |
string | A collection of characters strung together | "Hello World!" |
From the table above, only the first four of these data types - boolean
, int
, float
, and char
- are available to us out of the box.
We've seen we were able to use the string
data type in the previous lesson. That worked because the code we included at the top of our file loaded in support for string
objects.
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 that we will use in our code.
There are some restrictions to these names. The most notable 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 gets more complex, 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 capitalization, for example: currentHealth
, CurrentHealth
, or Current_Health
.
What would be a good name for a variable that stores whether or not a character is alive?
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 | isAlive | Is the monster currently alive? | true |
int | Health | How much health does the monster have 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 out of the box, because they’re useful across a wide range of programs.
Data types like position
and weapon
are much more specific to our needs.
So, they're not available to us by 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.
In this lesson, we introduced the basic concept of Object-Oriented Programming, covering:
bool
, int
, float
, char
, and string
, highlighting C++ as a strongly typed language.In the next lesson, we’ll put this knowledge to practical use. We will:
An introduction to the building blocks of our software - objects, and the variables that can be associated with them.
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way