Let’s look at how to display a message
- The myfirst.cpp program (A-1)
- Uses the following C++ statement (A-2):
- 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 is a particular instance of a class, and a class defines how data is stored and used.)
- It’s a predefined object that knows how to display a variety of things, including strings, numbers, and individual characters
Example of (A-1)
// myfirst.cpp--displays a message #include <iostream> // a PREPROCESSOR directive int main() // function header { // start of function body using namespace std; // make definitions visible cout << "Come up and C++ me some time."; // message cout << endl; // start a new line cout << "You won't regret it!" << endl; // more output return 0; // terminate main() }
Example of (A-2)
cout << "Come up and C++ me some time.";
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
- You don’t have to know the innards of an object in order to use it
The cout object has a simple interface
- If string represents a string, you can do the following to display it (A-3):
Example of (A-3)
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
- The cout object, whose properties are defined in the iostream file, represents that stream
Consider the following statement (note the terminating semicolon) (A-4)
- 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 (Image-1)
- Somehow, that sounds more impressive
- Thus, rather than say that your program displays a message, you can say that it inserts a string into the output stream
Example of (A-4)
cout << "Come up and C++ me some time.";
(Image-1) Displaying a string by using cout

A First Look at Operator Overloading
If you’re coming to C++ from C
- You probably noticed that the insertion operator (<<) looks just like the bitwise left-shift operator (<<)
- This is an example of operator overloading, by which the same operator symbol can have different meanings
- The compiler uses the context to figure out which meaning is intended
- This is an example of operator overloading, by which the same operator symbol can have different meanings
C itself has some operator overloading
- For example, the & symbol represents both the address operator and the bitwise AND operator
- The * symbol represents both multiplication and dereferencing a pointer
- The important point here is not the exact function of these operators but that the same symbol can have more than one meaning, with the compiler determining the proper meaning from the context
- (You do much the same when you determine the meaning of “sound” in “sound card” versus “sound financial basis.”)
- The important point here is not the exact function of these operators but that the same symbol can have more than one meaning, with the compiler determining the proper meaning from the context
- The * symbol represents both multiplication and dereferencing a pointer
C++ extends the operator overloading concept
- By letting you redefine operator meanings for the user-defined types called classes

Discussion
No comments for “C++ Output with cout”