【正文】
類型的類和對象在 iostream 中定義,寬字符文件流類型在 fstream 中定義,而寬字符 stringstream 則在 sstream 頭文件中定義。此外,標(biāo)準(zhǔn)庫還定義了一組相關(guān)的類型,支持 wchar_t 類型。這些類型可用于讀寫文件或 string 對象。我們曾經(jīng)編寫過的讀 istream 對象的程序也可用于讀文件(使用 ifstream 類型 )或者 string 對象(使用 istringstream 類型)。 C++ 中所提及的父類稱為 基類( base class) ,而繼承而來的類則稱為 派生類( derived class) 。 從概念上看,無論是設(shè)備的類型還是字符的大小,都不影響需要執(zhí)行的 IO 操作。 附錄 A 會介紹如何控制 IO 操作的格式、文件的隨機訪問以及無格式的 IO。 ? cin (發(fā)音為 seein):讀入標(biāo)準(zhǔn)輸入的 istream 對象。標(biāo)準(zhǔn)庫定義了一族類型,支持對文件和控制窗口等設(shè)備的讀寫( IO)。ve written that read an istream could be used to read a file (using the ifstream type) or a string (using the istringstream type). Similarly, programs that did output could use an ofstream or ostringstream instead of ostream. In addition to the istream and ostream types, the iostream header also defines the iostream type. Although our programs have not used this type, we actually know a good bit about how to use an iostream. The iostream type is derived from both istream and ostream. Being derived from both types means that an iostream object shares the interface of both its parent types. That is, we can use an iostream type to do both input and output to the same stream. The library also defines two types that inherit from iostream. These types can be used to read or write to a file or a string. Using inheritance for the IO types has another important implication: As we39。ll have more to say about inheritance and objectoriented programming in Part IV, but generally speaking, types related by inheritance share a mon interface. When one class inherits from another, we (usually) can use the same operations on both classes. More specifically, when two types are related by inheritance, we say that one class inherits the behavior the interface of its parent. In C++ we speak of the parent as the base class and the inheriting class as a derived class. The IO types are defined in three separate headers: iostream defines the types used to read and write to a console window, fstream defines the types used to read and write named files, and sstream defines the types used to read and write inmemory strings. Each of the types in fstream and sstream is derived from a corresponding type defined in the iostream header. Table lists the IO classes and Figure on the next page illustrates the inheritance relationships among these types. Inheritance is usually illustrated similarly to how a family tree is displayed. The topmost circle represents a base (or parent) class. Lines connect a base class to its derived (or children) class(es). So, for example, this figure indicates that istream is the base class of ifstream and istringstream. It is also the base class for iostream, which in turn is the base class for sstream and fstream classes. Because the types ifstream and istringstream inherit from istream, we already know a great deal about how to use these types. Each program we39。 others occur deep within the system and are beyond the scope of a program to correct. The IO library manages a set of condition state members that indicate whether a given IO object is in a usable state or has encountered a particular kind of error. The library also defines a set of functions and flags, listed in Table , that give us access to and let us manipulate the state of each stream. If we enter Borges on the standard input, then cin will be put in an error state following the unsuccessful attempt to read a string of characters as an int. Similarly, cin will be in an error state if we enter an endoffile. Had we entered 1024, then the read would be successful and cin would be in a good, nonerror state. To be used for input or output, a stream must be in a nonerror state. The easiest way to tes