|

Even
the simple program HELLO.CPP in Unit 1, "Getting
Started," 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:
into 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.
The first character is the # symbol, which is a
signal to the preprocessor. Each time you start
your compiler, the preprocessor is run. The preprocessor
reads through your source code, looking for lines
that begin with the pound symbol (#), and acts on
those lines before the compiler runs.
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. 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.
New
Term: The
preprocessor runs before your compiler each time
the compiler is invoked. The preprocessor translates
any line that begins with a pound symbol (#) into
a special command, getting your code file ready
for the compiler.
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. Usually functions are invoked
or called by other functions, but main() is special.
When your program starts, main() is called automatically.
main(),
like all functions, must state what kind of value
it will return. The return value type for main()
in HELLO.CPP is void, which means that this function
will not return any value at all. Returning values
from functions is discussed in detail in unit 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 in Unit 6,
"Basic Classes," These two objects, cout and cin,
are used in C++ to print strings and values to the
screen. A string is just a set of characters.
Here’s
how cout is used: type the word cout, followed by
the output redirection operator (<<). Whatever
follows the output redirection operator is written
to the screen. If you want a string of characters
written, be sure to enclose them in double quotes
("), as shown on line 5.
New
Term: 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!
All
ANSI-compliant programs declare main() to return
an int. This value is "returned" to the operating
system when your program completes. Some programmers
signal an error by returning the value 1. In this
book, main() will always return 0.
The
main() function ends on line 7 with the closing
brace.
|