Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

An Introduction to Object-Oriented Programming for C++ Game Developers

  • Print
  • + Share This
Before you can start coding your own games, you're going to need to know a little bit about C++. This chapter offers an introduction to C++ with a focus on creating games.
This chapter is from the book

Before we start writing games, we should talk a bit more about C++. Specifically, we need to discuss how to create your own data types, how to write conditions, and how to use if-else statements to make decisions. In addition, I'll explain how to keep the type names you create from conflicting with other type names in your game.

Software Objects

chapter 2 showed that C++ programs use variables to store information. Every variable has a data type. As you may recall, the purpose of the data type is to specify what kind of information the variable holds. For instance, integer variables hold integers (numbers with no fractional parts), floating-point numbers contain real numbers (numbers with fractional parts), strings hold groups of characters, and so on.

The C++ language would be extremely limited if it could only use the small group of data types it defines. We would have a tough time writing our programs that way. As game programmers, we usually need to create data types that represent things the player sees on the screen.

For example, when your game shows a dragon on the screen your code should be able to define a variable of type dragon. The dragon variable would store all the information needed to represent a dragon in the program. This information might include the dragon's current position, how far it can spit fire, and how many fireberries it's eaten (more fireberries means it spits fire farther). When the dragon moves, the dragon variable must keep track of which way it's moving and how fast.

To define our own types to represent things we need in games, we use a technique called object-oriented programming. In object-oriented programming, you define your types to represent anything you want. When you declare a variable of that type, you are said to be creating a software object. The objects you create can be anything in the real world or anything you can imagine. If you want to define an object to represent dragons, you can. If you need an object to represent walls, cars, feet, or trees, you can define those too. Anything can become objects in software—you're limited only by your own imagination.

Software objects contain data. The data describes the thing the object represents. In our dragon example, a dragon variable stores such information as the dragon's current position, how far it can spit fire, and how many fireberries it's eaten. That information describes a dragon. This is the way software objects work. We use them so that we can create variables that store all the information needed to describe an object.

All software objects behave exactly the way you tell them to. You define the set of actions, also called operations, that the software can perform on your objects. For example, consider the case of integers. What operations can we perform on integers?

Because we all know basic math, you and I can easily state that the most fundamental operations we can perform on integers are addition, subtraction, multiplication, and division. In C++, these four operations are built into the int data type. We saw how to use them in chapter 2.

All of the data types in C++ have a specific group of operations that programs can perform on them. If you make your own software objects, you're making your own data types. When you do, you must also define the set of operations that programs can perform on them. All of this is accomplished with C++ classes, so that's what we'll discuss next.

  • + Share This
  • 🔖 Save To Your Account