C++ Objects and Variables

An introduction to the building blocks of our software - objects, and the variables that can be associated with them.
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

Ryan McCombe
Ryan McCombe

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.

What are Objects?

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:

  • Heroes
  • Dungeons
  • Monsters
  • Items

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".

Models and Modeling

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:

  • What is its name?
  • Is it dead?
  • How much health does it have?
  • How much armor/damage resistance does it have?
  • Where is it currently located in the world?
  • What weapon does it have equipped?

Answers to these questions would be stored as variables on each monster object.

Data Types

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.

Strongly Typed Languages

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 TypeDescriptionExamples
boolA boolean - either true or falsetrue, false
intAn integer - a whole number42, 5, -621, 0
floatA decimal ("floating point") number3.4, -2.71, 4.0
stringA collection of characters strung together"Hello World!"

A Note on Strings

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() {


Variable Names

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.

Test your Knowledge

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 TypeVariable NameDescriptionExample
boolisDeadIs the monster currently dead?false
intHealthHow much health the monster has remaining150
floatArmorHow much damage resistance the monster has0.2
stringNameThe monster's name"Goblin Warrior"
positionLocationCoordinates of where the monster is in the 3D worldx=45.2, y=152.5, z=90.1
weaponWeaponThe monster's currently equipped weaponSteel 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!

Was this lesson useful?

Ryan McCombe
Ryan McCombe
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

C++ Fundamentals
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

Creating Variables in C++

Creating variables to store and update the data that describes our objects. We also introduce comments, allowing us to describe our code in plain language.
3D art showing a fantasy RPG character
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved