DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
iostream examples

iostream examples

C and C++ share the property that they do not contain any special input or output statements. Instead, I/O is implemented using ordinary mechanisms and standard libraries. In C this is the stdio library. In C++ it is the iostream library. Because C++ is an extension of C it is possible for a C++ program to use stdio. Using stdio may be the easiest way for a C programmer to get started with C++, but using stdio is not a good style for C++ I/O. Its main drawbacks are its type insecurity and the inability to extend it consistently for user defined classes.

This topic consists mainly of examples of the use of parts of the iostream library. It assumes a reasonable familiarity with C++, including such extensions to C as references, operator overloading, and the like. An attempt has been made to create examples that not only illustrate features of the iostream library, but represent good programming style. A programmer who is new to C++ may copy the examples ``cookbook style,'' but cannot be said to have mastered C++ until he or she understands the examples.

Some of the examples are moderately complicated and demonstrate advanced features of the iostream library. These are included so that the document will continue to be useful as an aid even after the programmer has written a few programs using iostreams.

This document is not a complete description of the iostream library. Some classes and members are not described at all. Some are used without complete descriptions. The reader is referred to the iostream man pages for more details.

The declarations for the iostream library exist in several header files. To use any part of it, a program should include iostream.h. Other header files may be needed for other operations. These are mentioned below, but the #include lines are never put in the examples.

The iostream library is divided into two levels. The low level (based on the streambuf class) is responsible for producing and consuming characters. This level is an independent abstraction and may be used without the upper level. This is appropriate when the program is moving characters around without much (or any) formatting operations.

The upper level is responsible for formatting. There are three significant classes. istream and ostream are responsible for input and output formatting, respectively. They are both derived from class ios, which contains members relating to error conditions and the interface to the low level. A third class, iostream, is derived (multiple inheritance) from both istream and ostream. It plays only a
minor role in the library. A ``stream class'' is any class derived from istream or ostream.

This topics covers:

The iostream library described here is similar, but not identical, to the iostream library being specified in the upcoming ANSI/ISO C++ language standard.


Next topic: Output

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005