cout
Now let’s look at how to display a message. The myfirst.cpp
program uses the following C++ statement:
cout << "Come up and C++ me some time.";
The part enclosed within the double quotation marks is the message to print. In C++, any series of characters enclosed in double quotation marks is called a character string, presumably because it consists of several characters strung together into a larger unit. The <<
notation indicates that the statement is sending the string to cout
; the symbols point the way the information flows. And what is cout
? It’s a predefined object that knows how to display a variety of things, including strings, numbers, and individual characters. (An object, as you might remember from Chapter 1, is a particular instance of a class, and a class defines how data is stored and used.)
Well, using objects so soon is a bit awkward because you won’t learn about objects for several more chapters. Actually, this reveals one of the strengths of objects. You don’t have to know the innards of an object in order to use it. All you must know is its interface—that is, how to use it. The cout
object has a simple interface. If string
represents a string, you can do the following to display it:
cout << string;
This is all you must know to display a string, but now take a look at how the C++ conceptual view represents the process. In this view, the output is a stream—that is, a series of characters flowing from the program. The cout
object, whose properties are defined in the iostream
file, represents that stream. The object properties for cout
include an insertion operator (<<
) that inserts the information on its right into the stream. Consider the following statement (note the terminating semicolon):
cout << "Come up and C++ me some time.";
It inserts the string “Come up and C++ me some time.” into the output stream. Thus, rather than say that your program displays a message, you can say that it inserts a string into the output stream. Somehow, that sounds more impressive (see Figure 2.2).
endl
Now let’s examine an odd-looking notation that appears in the second output statement in Listing 2.1:
cout << endl;
endl
is a special C++ notation that represents the important concept of beginning a new line. Inserting endl
into the output stream causes the screen cursor to move to the beginning of the next line. Special notations like endl
that have particular meanings to cout
are dubbed manipulators. Like cout
, endl
is defined in the iostream
header file and is part of the std
namespace.
Note that the cout
facility does not move automatically to the next line when it prints a string, so the first cout
statement in Listing 2.1 leaves the cursor positioned just after the period at the end of the output string. The output for each cout
statement begins where the last output ended, so omitting endl
would result in this output for Listing 2.1:
Come up and C++ me some time.You won't regret it!
Note that the Y
immediately follows the period. Let’s look at another example. Suppose you try this code:
cout << "The Good, the";
cout << "Bad, ";
cout << "and the Ukulele";
cout << endl;
It produces the following output:
The Good, theBad, and the Ukulele
Again, note that the beginning of one string comes immediately after the end of the preceding string. If you want a space where two strings join, you must include it in one of the strings. (Remember that to try out these output examples, you have to place them in a complete program, with a main()
function header and opening and closing braces.)
C++ has another, more ancient, way to indicate a new line in output—the C notation
:
cout << "What's next? "; // means start a new line
The
combination is considered to be a single character called the newline character.
If you are displaying a string, you need less typing to include the newline as part of the string than to tag an endl
onto the end:
cout << "Pluto is a dwarf planet.
"; // show text, go to next line
cout << "Pluto is a dwarf planet." << endl; // show text, go to next line
On the other hand, if you want to generate a newline by itself, both approaches take the same amount of typing, but most people find the keystrokes for endl
to be more comfortable:
cout << "
"; // start a new line
cout << endl; // start a new line
Typically, this book uses an embedded newline character (
) when displaying quoted strings and the endl
manipulator otherwise. One difference is that endl
guarantees the output will be flushed (in, this case, immediately displayed onscreen) before the program moves on. You don’t get that guarantee with "
"
, which means that it is possible on some systems in some circumstances a prompt might not be displayed until after you enter the information being prompted for.
The newline character is one example of special keystroke combinations termed “escape sequences”; they are further discussed in Chapter 3, “Dealing with Data.”
13.58.121.8