The next step of this lab is to scan through heatwave.cxx.
Using a text editor, open the file heatwave.cxx. Examine the program and
observe the following aspects:
- Comments in C++ are indication by two slashes in a row.
The entire line, after the // is a comment.
- The program contains two include directives. These tell
the compiler to include standard C++ libraries.
#include <iostream.h> // Provides cin, cout
#include <stdlib.h> // Provides EXIT_SUCCESS
These directives will appear in most C++ programs. The
library iostream.h provides standard input and output functions and devices
such as the standard input. The library stdlib.h provides several other
standard items such as a constant named EXIT_SUCCESS that our program uses.
- After the two include directives, the program declares
a named constant of type double called PI.
const double PI = 3.14159;
Because PI is declared as a constant, the compiler will
disallow statements that change its value. This is an important way to
give symbolic names to significant values and to prevent erroroneous modifications
to these values.
- Search through the code and find the heading of the main
program indicated by the symbol "main". Notice that
the main program returns a value of type int. The role of this
value is to inform the operating system about the success of the program's
execution. For the g++ compiler, a main program returns the number 0 to
indicate successful termination. For our g++ compiler, this "successful
return value" is defined in stdlib.h as a constant called EXIT_SUCCESS.
Look for the return statement in heatwave's main program that indicates
successful termination.
- Basic output is simple in C++. A common form of the output
statement looks like this:
cout << "How big is your tree?" <<
endl;
The name cout is the "console output device"--
i.e. the screen. The operator << is the output operator. So this
statement sends the string "How big is your tree?" to the console
output device, and then the special object, endl, is sent to the
console output device. The object endl signifies the end of a
line. It also serves to "flush" the output (meaning that the
output will be written then and there, rather than waiting for the output
buffer to fill).
- Basic input is also very simple in C++. A common form
of the input statement looks like this:
cin >> radius;
The name cin is the "console input device"--
i.e. the keyboard. The operator >> is the input operator. Thus, this
statement reads a real number set the variable radius to its value.