<I>cout</I>: The Black Box


Now that we know how to include the headers for printing to the screen and reading from the keybord, we're ready to talk about cout, the black box. Why do we say that? Well, cout (pronounced "see-out") is an object in C++ that we can't really say how it works. All we know is that if we give it data, it can put it on the screen. Because of this lack of knowledge about what's "inside the box", we say it's a black box.

So how do we give this cout thing data, you ask? This is the neat part. You use a syntax that really looks like what it does. Let's try using cout (which is defined in iostream.h) in a small program. Take a look:

#include <iostream.h>
void main(void)
{
  cout << "Hello, World!";
}

Note: You might not recognize a bit of the stuff here. Don't worry about it, all will be revealed in chapter 3. Think of the stuff you don't recognize as the "Begin" and "End" of C++.

Notice the << symbols? They are symbolic of arrows, and might give the impression that the string "Hello, World!" is pointing to cout. This is exactly what's happening! The symbol means to pass data from one thing to another, in this case, passing "Hello, World!" to cout, which somehow manages to print out that string. The end result is the same as this Pascal statement:

write('Hello, World!');

Of course, as the above Pascal may have tipped you off, this statement does not do a carriage return at the end to go down to the next line. How can we do that? Luckily, C++ defines another mystery object, called endl(short for end-line, as in "end the line"), that we can pass to cout to move the cursor down to the next line. So, let's try it!

cout << "Hey World! It's me again. Got any Tacos?";
cout << endl;


Table of Contents

Hello, World! | The Pre-Processor | Headers | cout: The Black Box
More Wonders of cout | cin: cout's evil twin | Exercises