Sams Teach Yourself C++ in 21 Days

(singke) #1
to be printed to the console. Because no newline character is present after the first
string, the next value is printed immediately afterward. This is called concatenating the
two values.
On line 7, an informative message is printed, and then the manipulator std::endlis
used. The purpose of endlis to write a new line to the console. (Other uses for endlare
discussed on Day 16, “Advanced Inheritance.”) Note that endlis also provided by the
standard library; thus,std::is added in front of it just as std::was added for cout.

30 Day 2


endlstands for end line and is end-ell rather than end-one. It is commonly
pronounced “end-ell.”
Use of endlis preferable to the use of ‘\n’, because endlis adapted to the
operating system in use, whereas ‘\n’ might not be the complete newline
character required on a particular OS or platform.

NOTE

On line 10, a new formatting character,\t, is introduced. This inserts a tab character and
is used on lines 10 to 16 to line up the output. Line 10 shows that not only integers, but
long integers as well, can be printed. Lines 13 and 14 demonstrate that coutwill do sim-
ple addition. The value of 8+5is passed to couton line 14, but the value of 13 is printed.
On line 15, the value 5/8is inserted into cout. The term (float)tells coutthat you
want this value evaluated as a decimal equivalent, and so a fraction is printed. On line
17, the value 7000 * 7000is given to cout, and the term (double)is used to tell cout
that this is a floating-point value. All this will be explained on Day 3, “Working with
Variables and Constants,” when data types are discussed.
On lines 18 and 20, you should have substituted your name for Jesse Liberty. If you
do this, the output should confirm that you are indeed a C++ programmer. It must be
true, because the computer said so!

Using the Standard Namespace ............................................................................


You’ll notice that the use of std::in front of both coutand endlbecomes rather dis-
tracting after a while. Although using the namespace designation is good form, it is
tedious to type. The ANSI standard allows two solutions to this minor problem.
The first is to tell the compiler, at the beginning of the code listing, that you’ll be using
the standard library coutand endl, as shown on lines 5 and 6 of Listing 2.3.
Free download pdf