Objects, Variables and Types
An introduction to the building blocks of our software - objects, and the variables that can be associated with them.
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 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 simplifies the design of complex software and 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 piece of the environment as distinct objects. This modular structure mirrors real-world interactions, making our software 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 be creating:
- 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 in the next chapter.
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 are:
- What is the monster's 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 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. That weapon object would have its own set of variables, like its damage and durability.
For now, the key point is that each variable stores data of a specific type. When creating programs, we need to be aware of the type of data that is being stored in the variables we're working with. Some common data types in C++ include:
| Data Type | Description | Examples |
|---|---|---|
bool | A boolean value - 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!" |
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 restrictions are that an identifier cannot contain a space, and it cannot start with a number. Aside from that, 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 a name like MyVariable or x.
There's no technical reason why we can't use names like MyVariable or x but, once our code gets more complex, it can be very difficult to follow what's happening if our variables do not have meaningful names.
When we want to use multiple words to describe our variable, there are some common conventions to work around the inability to use spaces. Common approaches include using underscores or capitalization. For example, currentHealth, CurrentHealth, and current_health are all valid names.
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 hypothetical monster objects, and map the things we need to track 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. Simple types like numbers and strings are included with the language by default 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 included as part of the language. However, C++ gives us the ability to add our own data types, which we can configure as needed based on whatever we're trying to build. We'll see how to do that in Chapter 3.
Until then, we will just use simple booleans, integers, floating-point numbers, and strings. This will help us establish the basics.
Summary
In this lesson, we introduced the basic concept of Object-Oriented Programming, covering:
- Objects: Digital representations of entities, essential for organizing complex software.
- Variables and Functions: These are the properties of objects, and the actions they can perform.
- Variable Naming: Giving our variables descriptive names helps keep our code clear and easy to work with.
- Data Types: C++ is a strongly typed language. Types like
bool,int,float,char, andstring, are available by default, and we can add our own data types.
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.