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 basic_ name. These
are the template names; the specializations have the more familiar names
(e.g., istream specializes basic_istream<char>).
![]() |
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
