-
Comments in C++ are indicated by two slashes in a row. The entire line,
after the // is a comment.
-
The program has two include directives that include two C++
libraries:
#include <iostream> // Provides cin, cout
#include <cstdlib> // Provides EXIT_SUCCESS
These directive will appear in most every C++ program. The library
iostream provides standard input and output functions and devices such
as the standard input. The library cstdlib 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 double number
named PI. But PI is no ordinary variable; it is a constant variable,
declared as shown here:
const double PI = 3.14159;
A constant declaration, such as this, is a new feature of C and C++. They
are like ordinary variable declarations that are given an initial value,
but because they are declared as a const, the compiler will prevent you
from writing any statements that change the value of this variable. In
this class, our programming style requires the names of all constants to
be written in capital letters, allowing constants to be easily spotted.
-
Search through the code and find the heading of the main program,
which
begins with the words "int ". The word int indicates that the
main program computes an integer value and returns this value to
the Windows operating system. This may
be different than your textbook--many programmers simply
declare the main program as a void function (which means that it
does
not give any final value back to the operating system).
But we suggest that your main program returns an int value, since this
integer can be used to tell the operating system whether the program was
successful or not. For our compilers, a main program returns the number 0
to indicate successful termination. For our g++ compiler, this
"successful return value" is defined in cstdlib as a constant called
EXIT_SUCCESS. Can you find the return statement in heatwave's main
program, which indicates successful termination? (Be warned: older
compilers on our DEC machines don't define EXIT_SUCCESS. If you are using
an older machine some day, you can still compile by adding the string
-DEXIT_SUCCESS=0 immediately after the g++).
-
Some output is quite simple in C++. A basic output statement looks like
this:
cout << "How big is your tree?" << endl;
The name cout is the "console output device"--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 is an
end-of-line, and it also serves to "flush" the output (meaning that the
output will be written then and there, rather than waiting for some
output buffer to fill up).
-
Some input is also quite simple in C++. A basic input statement looks like
this:
cin >> radius;
The name cin is the "console input device"--the keyboard. The operator
>>
is the input operator. So this statement inputs a real number into the
variable radius. You'll learn a lot more about input/output in other
labs, but this will do for now.