Previous Table of Contents Next


Chapter 2
The Parts of a C++ Program

Before we dive into the details of C++, classes, variables, and so forth, let’s take an hour to get a sense of how a program fits together. In this hour you will learn:

  The parts of a C++ program
  How the parts work together
  What a function is and what it does

The Parts of a Simple Program

The simple program from the first hour, HELLO.CPP, had many interesting parts. This section will review this program in more detail. Listing 2.1 reproduces the original version of HELLO.CPP for your convenience.

Listing 2.1 HELLO.CPP Demonstrates the Parts of a C++ Program


1: #include <iostream.h>
2:
3: int main()
4: {
5:    cout << “Hello World!\n”;
6:    return 0;
7: }

Hello World!
On line 1 the file IOSTREAM.H is included in the file. As far as the compiler is concerned, it is as if you typed the entire contents of the file IOSTREAM.H right into the top of HELLO.CPP.

Examining the #include, Character by Character

The first character is the # symbol, which is a signal to the preprocessor. What the heck is the preprocessor?

When you run your compiler, it first calls another program—the preprocessor. You don’t have to invoke the preprocessor directly; it is called automatically each time you run the compiler.

The job of the preprocessor is to read through your source code looking for lines that begin with the pound symbol (#). Each time it finds a line beginning with the pound symbol, the preprocessor modifies the code. It is the modified code that is given to the compiler.

The term include is a preprocessor instruction that says, “What follows is a filename. Find that file and read it in right here.” The angle brackets around the filename tell the preprocessor to “Look in all the usual places for this file.” If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file Iostream.H in the directory that holds all the H files for your compiler. These files are called “h files” or “include files” because they are included in source code files and end with the extension .h. The file Iostream.H (Input-Output-STREAM) is used by cout, which assists with writing to the screen.

The effect of line 1 is to include the file Iostream.H into this program as if you had typed it in yourself. By the time the compiler sees this file, the included file is right there, and the compiler is none the wiser.

Line by Line Analysis

Line 3 begins the actual program with a function named main(). Every C++ program has a main() function. In general, a function is a block of code that performs one or more actions. Functions are invoked, (some programmers say they are called) by other functions, but main() is special. When your program starts, main() is called automatically.

The function main(), like all functions, must state what kind of value it will return. Once again, main() is special; it will always return int. Returning a value from a function will be discussed in detail in Hour 4, “Expressions and Statements.”

All functions begin with an opening brace ({) and end with a closing brace (}). The braces for the main() function are on lines 4 and 7. Everything between the opening and closing braces is considered a part of the function.

The meat and potatoes of this program is on line 5. The object cout is used to print a message to the screen. We’ll cover objects in general beginning in Hour 7, “More About Classes.” The object cout and its related object cin are provided by your compiler vendor and enable the system to write to the screen and read in from the keyboard.

Here’s how cout is used: Write the word cout followed by the output redirection operator (<<). You create the output redirection operator by keyboarding shift-comma twice. Whatever follows the output redirection operator is written to the screen. If you want a string of characters to be written, be sure to enclose them in double quotes () as shown on line 5.

Note, a text string is a series of printable characters.

The final two characters, \n, tell cout to put a new line after the words Hello World!

On line 6 we “return” the value 0 to the operating system. On some systems this is used to signal to the operating system success or failure; the convention is to return 0 for success and any other number for failure. On modern windowing machines, this value is almost never used, and so all the programs in this book will return the value 0.

The main() function ends on line 7 with the closing brace.

Comments

When you are writing a program, it is always clear and self-evident what you are trying to do. Funny thing, though—a month later, when you return to the program, it can be quite confusing and unclear. I’m not sure how that confusion creeps into your program, but it’s always there.

To fight the onset of confusion and to help others to understand your code, you’ll want to use comments. Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program.


Previous Table of Contents Next