Conceptually, neither the kind of device nor the character size affects the IO operations we want to perform. For example, we’d like to use >>
to read data regardless of whether we’re reading a console window, a disk file, or a string
. Similarly, we’d like to use that operator regardless of whether the characters we read fit in a char
or require a wchar_t
.
The library lets us ignore the differences among these different kinds of streams by using inheritance. As with templates (§ 3.3, p. 96), we can use classes related by inheritance without understanding the details of how inheritance works. We’ll cover how C++ supports inheritance in Chapter 15 and in § 18.3 (p. 802).
Briefly, inheritance lets us say that a particular class inherits from another class. Ordinarily, we can use an object of an inherited class as if it were an object of the same type as the class from which it inherits.
The types ifstream
and istringstream
inherit from istream
. Thus, we can use objects of type ifstream
or istringstream
as if they were istream
objects. We can use objects of these types in the same ways as we have used cin
. For example, we can call getline
on an ifstream
or istringstream
object, and we can use the >>
to read data from an ifstream
or istringstream
. Similarly, the types ofstream
and ostringstream
inherit from ostream
. Therefore, we can use objects of these types in the same ways that we have used cout
.