< Previous | Contents | Next >

Displaying Text through the Standard

Output

The first line in the body of main() displays Game Over!, followed by a newline, in the console window.

std::cout << "Game Over!" << std::endl;

"Game Over!" is a stringa series of printable characters. Technically, its a string literal, meaning its literally the characters between the quotes.

cout is an object, defined in the file iostream, thats used to send data to the standard output stream. In most programs (including this one), the standard output stream simply means the console window on the computer screen.

Writing Your First Cþþ Program 9


I use the output operator (<<) to send the string to cout. You can think of the output operator like a funnel; it takes whatevers on the open side and funnels it to the pointy side. So the string is funneled to the standard outputthe screen.

I use std to prefix cout to tell the compiler that I mean cout from the standard library. std is a namespace. You can think of a namespace like an area code of a phone numberit identifies the group to which something belongs. You prefix a namespace using the scope resolution operator (::).

Finally, I send std::endl to the standard output. endl is defined in iostream and is also an object in the std namespace. Sending endl to the standard output acts like pressing the Enter key in the console window. In fact, if I were to send another string to the console window, it would appear on the next line.

I understand this might be a lot to take in, so check out Figure 1.3 for a visual representation of the relationship between all of the elements Ive just described.



image


image

Figure 1.3

An implementation of Standard Cþþ includes a set of files called the standard library, which includes the file iostream, which defines various things, including the object cout.