Formatted and unformatted output capabilities are provided by ostream. Capabilities for output include output of standard data types with the stream insertion operator (<<); output of characters via the put member function; unformatted output via the write member function (Section 15.5); output of integers in decimal, octal and hexadecimal formats (Section 15.6.1); output of floating-point values with various precision (Section 15.6.2), with forced decimal points (Section 15.7.1), in scientific notation and in fixed notation (Section 15.7.5); output of data justified in fields of designated widths (Section 15.7.2); output of data in fields padded with specified characters (Section 15.7.3); and output of uppercase letters in scientific notation and hexadecimal notation (Section 15.7.6).
15.3.1. Output of char * Variables
C++ determines data types automatically, an improvement over C. Unfortunately, this feature sometimes "gets in the way." For example, suppose we want to print the value of a char * to a character string (i.e., the memory address of the first character of that string). However, the << operator has been overloaded to print data of type char * as a null-terminated string. The solution is to cast the char * to a void * (in fact, this should be done to any pointer variable the programmer wishes to output as an address). Figure 15.3 demonstrates printing a char * variable in both string and address formats. Note that the address prints as a hexadecimal (base-16) number. [Note: The reader who wants to learn more about hexadecimal numbers should read Appendix D, Number Systems.] We say more about controlling the bases of numbers in Section 15.6.1, Section 15.7.4, Section 15.7.5 and Section 15.7.7. [Note: The memory address shown in the output of the program in Fig. 15.3 may differ among compilers.]
Figure 15.3. Printing the address stored in a char * variable.
(This item is displayed on page 776 in the print version)
1 // Fig. 15.3: Fig15_03.cpp 2 // Printing the address stored in a char * variable. 3 #include 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 char *word = "again"; 10 11 // display value of char *, then display value of char * 12 // static_cast to void * 13 cout << "Value of word is: " << word << endl 14 << "Value of static_cast< void * >( word ) is: " 15 << static_cast< void * >( word ) << endl; 16 return 0; 17 } // end main
|
15.3.2. Character Output using Member Function put
We can use the put member function to output characters. For example, the statement
cout.put( 'A' );
displays a single character A. Calls to put may be cascaded, as in the statement
cout.put( 'A' ).put( ' ' );
which outputs the letter A followed by a newline character. As with <<, the preceding statement executes in this manner, because the dot operator (.) evaluates from left to right, and the put member function returns a reference to the ostream object (cout) that received the put call. The put function also may be called with a numeric expression that represents an ASCII value, as in the following statement
cout.put( 65 );
which also outputs A.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography