Objects, Variables and Types

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

Free, Unlimited Access
3D art of a cleric character
Ryan McCombe
Ryan McCombe
Updated

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.

  • 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 video games, it could be dragons, weapons, and magical spells.

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:

  • Heroes
  • Dungeons
  • Monsters
  • Items

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.

Models and Modeling

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

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 alive?
  • 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. For example:

  • Whether the monster is alive will only have two possible values - it is either true or it is false
  • For information about its 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 another object entirely - a 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.

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
charAn individual character'a', 'B', '@'
stringA collection of characters strung together"Hello World!"

A Note on Strings

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

}

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

Test your Knowledge

Variable Naming

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 TypeVariable NameDescriptionExample
boolisAliveIs the monster currently alive?true
intHealthHow much health does the monster have 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 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.

Summary

In this lesson, we introduced the basic concept of Object-Oriented Programming, covering:

  • Objects in C++: Learned as digital representations of entities, essential for organizing complex software.
  • Variables and Functions: Introduced as properties and actions of objects.
  • Data Types: Introduced bool, int, float, char, and string, highlighting C++ as a strongly typed language.
  • Variable Naming: Discussed the importance of meaningful names for clarity in code.

What's Next?

In the next lesson, we’ll put this knowledge to practical use. We will:

  • Dive into creating and updating variables in code
  • Understand how data types affect variable declaration
  • Introduce comments, allowing us to add plain-language documentation to our code

Was this lesson useful?

Next Lesson

Creating Variables

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
Ryan McCombe
Ryan McCombe
Updated
Lesson Contents

Objects, Variables and Types

An introduction to the building blocks of our software - objects, and the variables that can be associated with them.

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
Types and Variables
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:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Creating Variables

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 © 2024 - All Rights Reserved