Review

We started out by discussing the tremendous reliability of computers. Whenever you hear “it's the computer's fault”, the overwhelming likelihood is that the software is to blame rather than the hardware. Then we took a look at the fact that although computers are calculating engines, many of the functions we use them for don't have much to do with numeric calculations; for example, the most common use of computers is probably Web browsing. Nevertheless, we started out our investigation of programming with numeric variables, which are easier to understand than non-numeric ones. To use variables, we need to write a C++ program, which consists primarily of a list of operations to be performed by the computer, along with directions that influence how these operations are to be translated into machine instructions.

That led us into a discussion of why and how our C++ program is translated into machine instructions by a compiler. We examined an example program that contained simple source code statements, including some that define variables and others that use those variables along with constants to calculate results. We covered the symbols that are used to represent the operations of addition, subtraction, multiplication, division, and assignment, which are +, , *, /, and = respectively. While the first four of these operators should be familiar to you, the last one is a programming notion rather than a mathematical one. This may be confusing because the operation of assignment is expressed by the = sign, but is not the same as mathematical equality. For example, the statement x = 3; does not mean “x is equal to 3”, but rather "set the variable x to the value 3”. After this discussion of the structure of statements in C++, we started an exploration of how the CPU actually stores and manipulates data in memory. The topics covered in this section included the order in which multibyte data items are stored in memory and the use of general registers to manipulate data efficiently.

Then we spent some time pretending to be a compiler, to see how a simple C++ program looks from that point of view, in order to improve our understanding of what the compiler does with our programs. This exercise involved keeping track of the addresses of variables and instructions and watching the effect of the instructions on the general registers and memory locations. During this exploration of the machine, we got acquainted with the machine language representation of instructions, which is the actual form that our executable programs take in memory. After a thorough examination of what the compiler does with our simplified example of source code at compile time, we followed what would happen to the registers and memory locations at run time (that is, if the sample code were actually executed).

Then we began to look at two data types that can hold nonnumeric data, namely the char and the string. The char corresponds to 1 byte of storage, and therefore can hold one character of data. Examples of appropriate values for a char variable include letters (a–z, A–Z), digits (0–9), and special characters (e.g., , . ! @ # $ %). A char can also represent a number of other “nonprintable” characters such as the “space”, which causes the output position on the screen to move to the next character. Actually, a char can also be used as a “really short” numeric variable, but that's mostly a holdover from the days when memory was a lot more expensive, and every byte counted.

One char doesn't hold very much information by itself, so we often want to deal with groups of them as a single unit; an example would be a person's name. This is the province of the string variable type: variables of this type can handle an indefinitely long group of chars.

At the beginning of our sample program for strings and chars, we encountered a new construct, the #include statement. This tells the compiler where to find instructions on how to handle data types such as strings, about which it doesn't have any built-in knowledge. Then we came across the line int main(), which indicates where we want to start executing our program. A C++ program always starts execution at the place indicated by such a line. We also investigated the meaning of int, which must always be the return type of main. This return type tells the compiler what sort of data this program returns to the operating system when it finishes executing; the return value can be used to determine what action a batch file should take next.

As we continued looking at the sample program for strings and chars, we saw how to assign literal values to both of these types, and noted that two different types of quotes are used to mark off the literal values. The single quote (') is used in pairs to surround a literal char value consisting of exactly one char, such as 'a'; and the double quote (") is used in pairs to surround a literal string value of the C string literal type, such as "This is a test".[43] We also investigated the reason for these two different types of literal values, which involves the notion of a null byte (a byte with the value 0); this null byte is used to mark the end of a C string literal in memory.

[43] The C string literal type is called that because it is inherited from C, which did not have a real string type such as the C++ string. These two types are quite different, although related in a way that we will see in a later chapter.

This led us to the discussion of the ASCII code, which is used to represent characters by binary values. We also looked at the fact that the same bytes can represent either a numeric value or a C string literal, depending on how we use those bytes in our program. That's why it's so important to tell the compiler which of these possibilities we have in mind when we write our programs. The way in which the compiler regulates our access to variables by their type is called the type system; C++ uses static type checking, in which types are determined at compile time rather than being deferred to run time (dynamic type checking). This is one of the reasons that C++ programs can be made more robust than programs written in languages that use dynamic type checking.

After a short discussion of some of the special characters that have a predefined meaning to the compiler, we took an initial glance at the mechanisms that allow us to get information into and out of the computer, known as I/O. We looked at the << function, which provides display on the screen when coupled with the built-in destination called cout. Immediately afterwards, we encountered the corresponding input function >> and its partner cin, which team up to give us input from the keyboard.

Next, we went over some program organization concepts including the if statement, which allows a program to choose between two alternatives; the while statement, which causes another statement to be executed while some condition is true; and the block, which allows several statements to be grouped together into one logical statement. Blocks are commonly used to enable several statements to be controlled by an if or while statement.

At last we were ready to write a simple program that does something resembling useful work, and we did just that. The starting point for this program, as with all programs, was to define exactly what the program should do; in this case, the task was to keep track of the weight of the heaviest pumpkin at a county fair. The next step was to define a solution to this problem in precise terms. Next, we broke the solution down into steps small enough to be translated directly into C++. Of course, the next step after that was to do that translation. Finally, we went over the C++ code, line by line, to see what each line of the program did.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset