Read an Excerpt
Chapter 4: C++ 101
Well, it's inevitable. If we are going to do anything useful with C++ we are
going to have to learn the syntax of the language. Because you already know Visual
Basic, it seems logical to use this knowledge as a crutch for learning C++,
which is what we will do throughout this chapter.
As you start to learn C++ it is important to remember that you already know
how to program—you just don't know the syntax of C++. Think of the process as
learning how to drive a new type of car for the first time. You already know how to
drive; you just have to learn where the trunk release is and how to set the clock in
the new car and you're as good as you were in your old car.
Data and Variables
Regardless of the language in which it was written, every computer program contains
some amount of data. Even if the program is only to write the words "Hello
World," the fact that a program runs on a computer is proof that the program contains
data. Thus, it is a good idea to start our learning of the C++ language by examining
the available C++ data types, their Visual Basic equivalents (where appli-cable),
and their storage capacity, which is summarized in Table 4-1.
You can see that there are many different data types available in C++. Also notice
that in most cases they map directly to Visual Basic types.
NOTE: The void data type in C++ is special in that it has no value. It is used
to show that there is an absence of value. The void type is usually used as
the return type of a function to denote that the function does not return a
value, or as the only item in a parameter list for a function that takes no
arguments.
Declaring Variables
Knowing the data types available in C++ is one thing, but we also need to know
how to declare variables in order to make use of these data types. The syntax for
variable declaration is in the form:
DataType VariableName [=InitialValue ];
An example declaration is as follows:
int x =0;
This statement assigns the value 0 to the variable x . In fact, any of the numeric
data types in C++ (which include short, int, long, float and double) can be
initialized in this way. Now you may have noticed that the keyword unsigned appears
before some of the types in our data type chart. This is because C++ supports
the idea of unsigned numbers. An unsigned number is simply a number
whose value cannot be less than zero. Unsigned numbers are useful for several
applications, such as representing binary data. (In contrast, Visual Basic has only
one unsigned data type, Byte, which covers the range 0 to 255.) For example, if I
wanted to assign the number 14 to x in my code, I would write the following
assignment statement:
unsigned int x =14;
The numeric data types are straight forward, but there are several data
types that require a little more explanation as to how they are used. We'll look at
those now.
NOTE: unsigned can prefix most C++ numeric data types.
The char Data Type
The char data type is quite flexible. It can be used for its namesake and hold a
single character with an assignment such as the following:
char myChar ='a';
Notice the use of single quotes surrounding the initializing value of a . When
you want to assign a single character to a variable of type char, you must always
surround it with single quotes. You may assume that because we told our program
to store the character a in memory that it did. Well, actually it didn't. Don't assume
that we have a renegade variable here; it is just that a computer is not able to store
characters—only numbers.
So what does get stored as the value of myChar? The number 61, which just
happens to be the hexadecimal equivalent of the number 97, which just happens
to be the ASCII (American Standard Code for Information Interchange) value for
the character a.
Make sense? Good. Now don't let this confuse you. The key thing to remember
is that all of the data in a computer is stored as numbers. It is the context in
which we use those numbers that generates meaning. Think of the saying, "One
man's trash is another man's treasure." To someone just looking at the memory
stored in our program, the number 61 doesn't mean anything. But, because we
know that 61 is the ASCII representation of a character, to us it means a lot. Just
remember that all data interpretation is relative to its intended use. (This concept
will start to make more sense when we cover the printf()function later in this
chapter.)
The char [] Data Type
The char[] data type is not a unique data type; it is just an array of char, or basically
a string of characters. We could use this data type to store any character string that is too large for the type char. (In other words, a string that is more than
one character long.) Following is an example usage of type char[]...