Thursday, September 27, 2007

The Structure of a C++ Program

Programs which will run as console applications under Visual C++ are text-based MS-DOS programs. All the examples that we'll write to understand how C++ works will be MS-DOS programs, so let's look at how such programs are structured.
A program in C++ consists of one or more functions. Every C++ program in the DOS environment contains the function main() and all C++ programs of any size consist of several functions. A function is simply a self-contained block of code with a unique name which is invoked by using the name of the function.
A typical DOS program might be structured as shown in the figure:
The figure above illustrates that execution of the program shown starts at the beginning of the function main(). From main(), execution transfers to a function input_names(), which returns execution to the position immediately following the point where it was called in main(). The function sort_names() is then called from main() and, once control returns to main(), the final function output_names() is called. Eventually, once output has been completed, execution returns once again to main() and the program ends.
Of course, different programs under DOS may have radically different functional structures, but they all start execution at the beginning of main(). The principal advantage of having a program broken up into functions is that you can write and test each piece separately. There is a further advantage in that functions written to perform a particular task can be re-used in other programs. The libraries that come with C++ provide a lot of standard functions that you can use in your programs. They can save you a great deal of work.
We'll see more about creating and using functions in Chapter 4.
Try It Out - A Simple Program
Let's look at a simple example to understand the elements of a program a little better. Start by creating a new project from the range of alternatives offered on the Projects tab when you click the New... item in the File menu. When the dialog appears, select Win 32 Console Application and name the project Ex1_01; then click on OK.
Next, the Console Application dialog appears. We'll start from the very basic project structure, so choose An empty project, and click on the Finish button.
The New Project Information tells you about the project you're about to create; it should look something like this:
The project directory at the foot of the screen may be slightly different on your machine; everything else should be identical. Once your happy with it, click on OK.
We'll start by entering the following program as a new source file, so click the leftmost icon on the standard toolbar, , to create a new text file into which you can type the code.
Note that this code is available for download from the Wrox Press website: http://www.wrox.com, as are all of the code examples in this tutorial.// EX1_01.CPP
// A Simple Example of a Program
#include
using namespace std;
int main()
{
int apples, oranges; // Declare two integer variables
int fruit; // ...then another one
apples = 5; oranges = 6; // Set initial values
fruit = apples + oranges; // Get the total fruit
cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
return 0; // Exit the program
}
The above example is intended to illustrate some of the ways in which you can write C++ statements, rather than to be a model of good programming style.
Note that in the word endl ends with the letter l, not with the numeral 1.
Once you have keyed it in, save it as Ex1_01.cpp using the File menu Save As... option. The Save As dialog will offer to save it in the project directory that you just created and, since this file is part of that project, that's the best place for it. Since you have now identified the file by its extension as a file containing C++ code, the keywords in the code will gain their colors. You will be able to see if you have entered Int where you should have entered int, since the two will be different colors.
If you look at the FileView tab for your new project, you'll notice that the source file doesn't appear as part of the project's files. We need to add the file to the project using the Add to Project... item from the Project menu, or by right-clicking in the window containing the source and selecting Insert File into Project. Once this is done, you'll see the newly created source file in the FileView, and the main() function will appear under the Globals section of the ClassView. We'll consider the meaning of this later.
If you now build this program by using the Build button on the Project toolbar, , and execute it using the Execute Ex1_01.exe item in the Build menu, or by pressing the Execute Program button, , you should get the following output:

Program Comments
The first two lines in the C++ code are comments. Comments are an important part of any program, but they're not executable code - they are there simply to help the human reader. All comments are ignored by the compiler. On any line of code, two successive slashes // that are not contained within a text string (we shall see what text strings are later) indicate that the rest of the line is a comment.
You can see that several lines of the program contain comments as well as program statements. You can also use an alternative form of comment bounded by /* and */. For example, the first line of the program could have been written:/* EX1_01.CPP */
The comment using // only covers the portion of the line following the two successive slashes, whereas the /*...*/ form defines whatever is enclosed as a comment and can span several lines. For example, we could write:/*
EX1_01.CPP
A Simple Program Example
*/
All four lines are comments. If you want to highlight some particular comment lines, you can always embellish them with a frame of some description:/*****************************
* EX1-01.CPP *
* A Simple Program Example *
*****************************/
As a rule, you should always comment your programs comprehensively. The comments should be sufficient for another programmer, or you at a later date, to understand the purpose of any particular piece of code, and to understand how it works.
The #include Directive - Header Files
Following the comments, we have the #include directive,#include
which makes the compiler insert the contents of the file iostream into the program before compilation. This file is called a header file because it's usually brought in at the beginning of a program file. This particular header file contains definitions that are necessary for you to be able to use input and output statements in C++. If we didn't include iostream in our program, it wouldn't compile because we use output statements which depend on some of the definitions in this file. There are many different header files provided by Visual C++ and they cover a wide range of capabilities. We shall be seeing more of them as we progress through the language facilities.
A #include statement is one of several preprocessor directives. The Visual C++ editor recognizes these and highlights them in blue in your edit window (as it does with all the C++ keywords). Preprocessor directives are commands executed by the compiler that generally act on your source code in some way before it is compiled. They all start with the # character. We'll be introducing other preprocessor directives as we need them.
The using Directive - Namespaces
The standard library is an extensive set of routines which have been written to do many common tasks: for example, dealing with input and output, performing basic mathematical calculations, etc. Instead of writing these routines for yourself, you can simply pluck them out of the standard library and use them in your code. The file iostream is just one of a number of header files that contain the routines of the standard library. (You can see a full list of the standard library files in the online MSDN help files in Visual C++ Documentation \ Reference \ C/C++ Language and C++ Libraries \ Standard C++ Library Reference.)
The code for all of these standard library routines is contained within the namespace std. So, any standard library routine belongs to the namespace std. Each of the standard library header files contributes a few routines to the namespace std.
The code in our program does not belong to the namespace std. Therefore, to use the output routines in the iostream header file, we need to tell the compiler that the routines belong to the namespace std. In order to do this, we use the using directive:using namespace std;
With this line in our program, the compiler knows that we will be using routines that belong to the standard library. There are other ways of dealing with this, and we'll see those later, when we'll go into namespaces in more detail in Chapter 5, when we will be able to create our own namespaces.
The Function main()
The function main() in our example consists of the function header defining it as main() plus everything from the first opening curly brace, {, to the corresponding closing curly brace, },. The curly braces enclose the executable statements in the function, which are referred to collectively as the body of the function.
As we shall see, all functions consist of a header which defines (amongst other things) the function name, followed by the function body which consists of a number of program statements enclosed between a pair of curly braces. The body of a function may contain no statements at all, in which case it doesn't do anything.
A function that doesn't do anything may seem somewhat superfluous, but when you're writing a large program, you may map out the complete program structure in functions but, initially, leave the code for many of them with empty bodies. Doing this means that you can compile and execute the whole program with all its functions at any time, but add detailed coding for the functions incrementally.
Program Statements
The program statements making up the function body of main() are each terminated with a semicolon. The program statement is the basic unit in defining what a program does. This is a bit like a sentence in a paragraph of text, where each sentence stands by itself in expressing an action or an idea, but relates to and combines with the other sentences in the paragraph in expressing a more general idea. A statement is a self-contained definition of an action that the computer is to carry out, but which can be combined with other statements to define a more complex action or calculation.
The action of a function is always expressed by a number of statements, each ending with a semicolon. Let's take a quick look at each of the statements in the example that we have just written, just to get a general feel for how it works. We will discuss each type of statement more fully later in this chapter.
The first statement in the program,int apples, oranges; // Declare two integer variables
declares two variables, apples and oranges. A variable is a named bit of computer memory that you can use to store data. A statement introducing the names of variables is called a variable declaration. The keyword int indicates that the variables are to store values that are whole numbers, or integers. The next statement declares another integer variable, fruit. While you can declare several variables in the same statement, as we did for apples and oranges, it is generally a good idea to declare them separately. This enables you to comment them individually.
In the example, the line,apples = 5; oranges = 6; // Set initial values
contains two statements, each terminated by a semicolon. While it isn't obligatory, it's good programming practice to write only one statement on a line. The two statements store the values 5 and 6 in the variables apples and oranges respectively. These statements are called assignment statements, because they assign a new value to a variable.
The next statement,fruit = apples + oranges; // Get the total fruit
is also an assignment statement. This one adds the values stored in the variables apples and oranges and stores the result in the variable fruit.
The next three statements are:cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
These are all output statements. The first sends a newline character, denoted by the word endl, to the screen. In C++, a source of input or a destination for output is referred to as a stream. The word cout specifies the 'standard' output stream, and the operator << indicates that what appears to the right of the operator is to be sent to the output stream, cout. The operator << 'points' in the direction that the data flows - from the variable or string on the right to the output destination on the left.
The meaning of the word cout and the operator << are defined by the contents of the header file iostream, which you'll remember we added to our program code by placing the #include directive at the beginning of the program. Because cout has been defined to send the standard output stream to your display screen, you shouldn't use the word cout for other purposes - for example, as a variable in your program.
The second statement sends a text string (defined between quotes) to the screen, followed by another newline character (endl), then another text string, followed by the value stored in the variable fruit, then finally another text string. There is no problem stringing together a sequence of things that you want to output in this way. The statement executes from left to right, with each item being sent to cout in turn. Note that each item is preceded by its own << operator.
The third statement sends another newline character to the screen. These statements produce the output from the program that you see. Note that the second statement runs over two lines. The successive lines are combined into a single statement until the compiler finds the semicolon that defines the end of the statement. This means that if you forget a semicolon for a statement, the compiler will assume the next line is part of the same statement and join them together. This usually results in something the compiler cannot understand, so you'll get an error when you try to compile the code.
The last statement in our program,return 0; // Exit the program
stops execution of the program and returns control to the operating system. We will be discussing all of these statements in more detail later on.
The statements in a program are executed in the sequence in which they are written, unless a statement specifically causes the natural sequence to be altered. In Chapter 3, we will look at statements which alter the sequence of execution.
Whitespace
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the statement,int fruit; // ...then another one
there must be at least one whitespace character (usually a space) between int and fruit for the compiler to be able to distinguish them. On the other hand, in the statementfruit = apples + oranges; // Get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish. This is because the = is not alphabetic or numeric, so the compiler can separate it from its surroundings. Similarly, no whitespace characters are necessary either side of the + sign.
Apart from its use as a separator between elements in a statement that might otherwise be confused, whitespace is ignored by the compiler (except, of course, in a string of characters between quotes). You can, therefore, include as much whitespace as you like to make your program more readable, as we did when we spread our output statement in the last example over several lines. In some programming languages, the end of a statement is at the end of the line, but in C++ the end of a statement is wherever the semicolon occurs.
Since variable names must be made up of single words, you must not put whitespace characters in the middle. If you do, the single variable name won't be seen by the compiler as such, and it won't be interpreted correctly.
Statement Blocks
We can enclose several statements between a pair of curly braces, in which case they become a block, or a compound statement. The body of a function is an example of a block. Such a compound statement can be thought of as a single statement (as we shall see when we look at the decision making possibilities in C++ in the next chapter. In fact, wherever you can put a single statement in C++, you could equally well put a block of statements between braces. As a consequence, blocks can be placed inside other blocks. In fact, blocks can be nested, one within another, to any depth.
A statement block also has important effects on variables, but we will defer discussion of this until later in this chapter when we discuss something called variable scope

No comments: