< Previous | Contents | Next >
So far you’ve seen how to store individual pieces of information in variables and how to manipulate those variables using operators and functions. But most of the things you want to represent in games—such as, say, an alien spacecraft— are objects. They’re encapsulated, cohesive things that combine qualities (such as an energy level) and abilities (for example, firing weapons). Often it makes no sense to talk about the individual qualities and abilities in isolation from each other.
Fortunately, most modern programming languages let you work with software objects (often just called objects) that combine data and functions. A data element of an object is called a data member, while a function of an object is called a member function. As a concrete example, think about that alien spacecraft. An alien spacecraft object might be of a new type called Spacecraft, defined by a game programmer, and might have a data member for its energy level and a member function to fire its weapons. In practice, an object’s energy level might be stored in its data member energy as an int, and its ability to fire
its weapons might be defined in a member function called fireWeapons().
Every object of the same type has the same basic structure, so each object will have the same set of data members and member functions. However, as an individual, each object will have its own values for its data members. If you had a squadron of five alien spacecrafts, each would have its own energy level. One might have an energy level of 75, while another might have a level of only 10, and so on. Even if two crafts have the same energy level, each would belong to a unique spacecraft. Each craft could also fire its own weapons with a call to its member function, fireWeapons(). Figure 3.2 illustrates the concept of an alien spacecraft.
88 Chapter 3 n For Loops, Strings, and Arrays: Word Jumble
Figure 3.2
This representation of the definition of an alien spacecraft says that each object will have a data member called energy and a member function called fireWeapons().
The cool thing about objects is that you don’t need to know the implementation details to use them—just as you don’t need to know how to build a car in order to drive one. You only have to know the object’s data members and member functions—just as you only need to know where a car’s steering wheel, gas pedal, and brake pedal are located.
You can store objects in variables, just like with built-in types. Therefore, you could store an alien spacecraft object in a variable of the Spacecraft type. You can access data members and member functions using the member selection operator (.), by placing the operator after the variable name of the object. So if you want your alien spacecraft, ship, to fire its weapons only if its energy level is greater than 10, you could write:
// ship is an object of Spacecraft type if (ship.energy > 10)
{
ship.fireWeapons()
}
ship.energy accesses the object’s energy data member, while ship.fireWeapons()
calls the object’s fireWeapons() member function.
Although you can’t make your own new types (like for an alien spacecraft) just yet, you can work with previously defined object types. And that’s next on the agenda.