Introduction to I/O Streams
As with C and many modern languages, input and output in C++ is implemented entirely in the library. No language features specifically support I/O.
The C++ I/O library is based on a set of templates parameterized
on the character type. Thus, you can read and write plain
char-type characters, wide
wchar_t characters, or some other, exotic
character type that you might need to invent. (Read about character
traits in Chapter 8 first.) Figure 9-1 depicts the class
hierarchy. Notice that the names are of the form
are the template names; the specializations have the more familiar names
One advantage of using inheritance in the I/O library is that the basic I/O functions are defined once in the base classes, and that interface is inherited by the derived classes and overridden when necessary. Using inheritance, you perform I/O with files as you would with strings. With only a little effort, you can derive your own I/O classes for specialized situations. (See Example 9-6.) Thus, to understand I/O in C++, you must start with the base classes.
Another advantage of the I/O stream classes is that you can implement your own overloaded functions that look and behave like the standard functions. Thus, you can read and write objects of your custom classes just as ...