The Parts of a Program
The program you created during the first hour, Motto.cpp, contains the basic framework of a C++ program. Listing 2.1 reproduces the source code of this program so that it can be explored in more detail.
When typing this program in to a programming editor such as NetBeans, remember not to include the line numbers in the listing. They are included solely for the purpose of referring to specific lines in this book.
LISTING 2.1 The Full Text of Motto.cpp
1: #include <iostream> 2: 3: int main() 4: { 5: std::cout << "Solidum petit in profundis!\n"; 6: return 0; 7: }
This program produces a single line of output, the motto of Aarhus University:
Solidum petit in profundis!
On line 1 of Listing 2.1 a file named iostream is included in the source code. This line causes the compiler to act as if the entire contents of that file were typed at that place in Motto.cpp.
Preprocessor Directives
A C++ compiler’s first action is to call another tool called the preprocessor that examines the source code. This happens automatically each time the compiler runs.
The first character in line 1 is the # symbol, which indicates that the line is a command to be handled by the preprocessor. These commands are called preprocessor directives. The preprocessor’s job is to read source code looking for directives and modify the code according to the indicated directive. The modified code is fed to the compiler.
The preprocessor serves as an editor of code right before it is compiled. Each directive is a command telling that editor what to do.
The #include directive tells the preprocessor to include the entire contents of a designated filename at that spot in a program. As you learned in Hour 1, “Writing Your First Program,” C++ includes a standard library of source code that can be used in your programs to perform useful functionality. The code in the iostream file supports input and output tasks such as displaying information onscreen and taking input from a user.
The < and > brackets around the filename iostream tell the preprocessor to look in a standard set of locations for the file. Because of the brackets, the preprocessor looks for the iostream file in the folder that holds header files for the compiler. These files also are called include files because they are included in a program’s source code.
The full contents of iostream are included in place of line 1.
The contents of the file iostream are used by the cout command in line 5, which displays information to the screen.
There are no other directives in the source code, so the compiler handles the rest of Motto.cpp.
Source Code Line by Line
Line 3 begins the actual program by declaring a function named main(). Functions are blocks of code that perform one or more related actions. Functions do some work and then return to the spot in the program where they were called.
Every C++ program has a main() function. When a program starts, main() is called automatically.
All functions in C++ must return a value of some kind after their work is done. The main() function always returns an integer value. Integers are specified using the keyword int.
Functions, like other blocks of code in a C++ program, are grouped together using the brace marks { and }. All functions begin with an opening brace { and end with a closing brace }.
The braces for the main() function of Motto.cpp are on lines 4 and 7, respectively. Everything between the opening and closing braces is part of the function.
In line 5, the cout command is used to display a message on the screen. The object has the designation std:: in front of it, which tells the compiler to use the standard C++ input/output library. The details of how this works are too complex for this early hour and likely will cause you to throw the book across the room if introduced here. For the safety of others in your vicinity, they are explained later. For now, treat std::cout as the name of the object that handles output in your programs and std::cin as the object that handles user input.
The reference to std::cout in line 5 is followed by <<, which is called the output redirection operator. Operators are characters in lines of code that perform an action in response to some kind of information. The << operator displays the information that follows it on the line. In line 5, the text "Solidum petit in profundis!\n" is enclosed within double quotes. This displays a string of characters on the screen followed by a special character specified by "\n", a newline character that advances the program’s output to the beginning of the next line.
On line 6, the program returns the integer value 0. This value is received by the operating system after the program finishes running. Typically, a program returns the value 0 to indicate that it ran successfully. Any other number indicates a failure of some kind.
The closing braces on line 7 ends the main() function, which ends the program. All of your programs use the basic framework demonstrated by this program.