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.

Factoid

One of the most common game programming tasks is creating software objects to represent things that players see on the screen.


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

Note

Because an object's data describes whatever the object represents, programmers often say that the set of data the object contains is its attributes.


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.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net