Home Segments Index Top Previous Next

73: Mainline

You can have several output operators in the same output statement, as in the following program:

#include  
main ( ) { 
  cout << "The volume of the box car is " << 11 * 9 * 40 << endl; 
} 
--- Result --- 
The volume of the box car is 3960 

Because the output operator associates left to right, the preceding program is equivalent to the following program:

#include  
main ( ) { 
  ((cout << "The volume of the box car is ") << 11 * 9 * 40) << endl; 
} 
--- Result --- 
The volume of the box car is 3960 

To see how C++ handles such output statements, you need to take a fresh look at cout and at the output operator. So far, you have viewed cout merely as a symbol that specifies that you want the data handed to the output operator to be displayed on your screen. More precisely, however, cout is a variable, and like all variables, it is the name of a chunk of memory, and that chunk of memory tells C++ the place where you want to put data.

Next, you need to know that each output expression refers to the same chunk of memory that is referred to by the output expression's left-side operand. Accordingly, the output expression cout << "The volume of the box car is " refers to a chunk of memory that specifies a place to put data, and that chunk of memory is the same chunk of memory that is named by cout.

              *--------------------------* 
              |                          | 
--------------*------------------------  |   
cout << "The volume of the box car is "  |   
-*--                                     |   
 |                                       |   
 *-------------------------------------* | 
                                       | | 
                                       v v 
                                      *-----*-----*-----*-----*-- 
                                      |     |     |     |     | 
                                      *-----*-----*-----*-----*---