Line 8 of Listing 2.1 declares a variable. Think of a variable as simply a
container, much like a mailbox. People put mail in mailboxes and take it out later. Each mailbox has an address. Variables work very much like that. Every byte in your computer's memory has an address, which is a number. Memory addresses can be specified either in binary or hexadecimal. Hexadecimal is yet another number systemit's base 16.
You and I are humans. We don't want to work with binary or hexadecimal address numbers if we can avoid it. It's far easier for us to give a memory location a name and use that instead of an address. We can let the compiler translate the variable name into an actual address in memory. Again, it's much like a mailbox. After a mail carrier has been on a mail route for a long time,
she knows that the address 1234 South 5th Street is where the Wobbenflatz family lives. When she sees a letter for the Wobbenflatzes, she can put it in the right mailbox without having to look up the address. She uses the name Wobbenflatz for the mailbox rather than the address 1234 South 5th Street. You and I do the same with variables in programs. We use the variable names rather than the addresses because they're easier for us to deal with.
The variable on line 8 is called yourName. It's a spot in memory where the
program stores your name. And once again, it works very much like a mailbox. The program can store your name in the variable and leave it there like people store mail in mailboxes. Later, when the program needs the data, it pulls the data out of yourName just as you'd retrieve your mail.
Notice that the variable name yourName describes the data that gets stored in the
variable. All variable names should be like this. Variable names must conform to the following rules:
Variable names must start with a letter. The letter can be uppercase or lowercase.
Most compilers let variable names be as long as 128 characters; some allow them to be longer. However, virtually all compilers require that all variable names be unique in the first 32 characters. So if two variable names use exactly the same letters in the same order for the first 32 characters, the compiler sees them as the same name. It does not check any more than 32 characters in the name.
After the first character, variable names may contain any letter or number.
Variable names can also contain the underscore character (_).
Variable names are case sensitive. Therefore, yourName is not the same as YourName or YOURNAME. The compiler sees these as three different variables.
C++ commands, which are called keywords, cannot be used as variable names. We'll talk more about C++ keywords later.
Every variable must have a data type. More on this in a moment. Spaces are not allowed.
If your variables do not follow these rules, your program won't compile.
As noted above, every variable must have a data type. The data type specifies exactly what kind of information the variable can hold. For instance, the
variable yourName on line 8 of Listing 2.2 is of type string. A string is a collection of
characters, such as the upper- and lowercase letters of the alphabet. As you read through this book, you'll see data types that contain integers, floating- point (real) numbers, logical values, and many other kinds of data. When you declare a variable to store each of these different kinds of data, you must
specify the appropriate data type. Table 2.1 shows many of the variable types that are built into C++.
Table 2.1. The Most Common C++ Built-In Data Types Type Description
int
Integer. A number with no decimal point. The values 0, 1, - 57, and 1000 are examples of integers. Integers usually contain 4 bytes of data.
short Short integer. Same as an integer, but it usually contains 2 bytes of data.
long
Long integer. Same as an integer and usually contains 4 bytes of data, but with some compilers, it can contain 8 bytes of data.
unsigned
Unsigned integer. Can only contain values that are greater than or equal to 0. The unsigned integer usually contains 4 bytes of data.
float
Floating-point number. A number with a decimal point. The values 0.0, 1.2,0.00012345, and -10.5 are all examples of floating-point numbers. Floating-point numbers usually contain 4 bytes of data.
double Double-sized floating-point number. Same as a floating- point number, but it usually contains 8 bytes of data.
char Character. Contains characters. The char type contains 1 byte of data.
bool Boolean value. Can be set to either true or false. The bool type usually contains 1 byte of data.
C++ Standard Libraries. The C++ Standard Libraries contains a huge number of very useful types. We'll discuss some of the most common as we write our example programs. You'll find more information on the C++ Standard Libraries in the books listed in Appendix C.
You may be wondering if you have to use the style of variable name that I use in this book. The answer is no. C++ does not force you to use a particular
style of variable name. As a result, you can create names such as yourName, your_name, YourName, and YOURNAME in your programs. This leads to
confusion for new programmers.
Some common questions they ask are:
What letters should I capitalize and what should be lowercase? Should I use underscores?
Do most people use the style in this book? Should I use Microsoft's Hungarian notation?
The most common style in C++ programming is the style I used in Listing 2.2, which is called camel notation. In this style, the first word of the variable is lowercase. All subsequent words start with capital letters.
Note
There are other data types built into C++ that are not presented in
Table 2.1. I'll discuss them later as needed. These are the data
types that you'll use most often in your games.
I strongly encourage you to use camel notation. Programmers tend to use it because everyone recognizes that anything in that notation is a variable. It's often the case that many programmers are working together on games. Other programmers on the project can just look at a variable name in camel notation and tell it's a variable. That helps clarify what they're reading.
Other variable name styles do exist. The one you'll run into most often is Microsoft's Hungarian notation. They created it for the C programming
language, which was the language that C++ is based on.
Hungarian notation puts an abbreviation at the beginning of the variable name that describes what type of data is in the variable. For example, a string
variable would be sYourName. However, for reasons that have to do with the C++ language, it could also be strYourName, cpYourName arrYourName, or even bpYourName. This shows one of the big problems with Hungarian
notationit's not standardized in the least.
Hungarian notation made sense in C, but it is not useful in C++ for a lot
reasons that I won't go into now. I'm just going to say that in C++, Hungarian notation causes more problems than it solves. You'll do well to avoid it.