This chapter gives an overview of the C++ Class Library, and includes the following sections:
The header files described below are supplied with the C++ class library. When an object from the library is to be manipulated with a function, the related header file should be included in the source file. The related header file is shown in the synopsis for the function. The header files provide the proper declarations for the functions and for the number and types of arguments used with them. Constant values and enumerations used in conjunction with the functions are also declared. The files can be included in any order.
The following files are provided with the software. The header files are all located in the /usr/include directory.
The .hpp extension is used to avoid colliding with the ANSI C string.h header file. |
The set of classes that make up the C++ class library use several common typedefs and macros. They are declared in iostream.h and fstream.h.
typedef long streampos; typedef long streamoff; typedef int filedesc; #define __NOT_EOF 0 #define EOF -1
The streampos type represents an absolute position within the file. For Watcom C++, the file position can be represented by an integral type. For some file systems, or at a lower level within the file system, the stream position might be represented by an aggregate (structure) containing information such as cylinder, track, sector and offset.
The streamoff type represents a relative position within the file. The offset can always be represented as a signed integer quantity since it's a number of characters before or after an absolute position within the file.
The filedesc type represents the type of a C library file handle. It's used in places where the I/O stream library takes a C library file handle as an argument.
The __NOT_EOF macro is defined for cases where a function needs to return something other than EOF to indicate success.
The EOF macro is defined to be identical to the value provided by the stdio.h header file.
Most programs interact in some manner with the keyboard and screen. The C programming language provides three values, stdin, stdout and stderr, that are used for communicating with these standard devices, and are opened before the user program starts execution at main(). These three values are FILE pointers, and can be used in virtually any file operation supported by the C library.
In a similar manner, C++ provides seven objects for communicating with the same standard devices. C++ provides the three C FILE pointers stdin, stdout and stderr, but they can't be used with the extractors and inserters provided as part of the C++ library. C++ provides four new objects, called cin, cout, cerr and clog, which correspond to stdin, stdout, stderr, and buffered stderr, respectively.
Extractions from standard input via cin skip whitespace characters by default because the ios::skipws bit is on. The default behavior can be changed with the ios::setf() public member function, or with the setiosflags() manipulator.
Insertions to standard output via cout are buffered by default because the ios::unitbuf bit is not on. The default behavior can be changed with the ios::setf() public member function, or with the setiosflags() manipulator.
Insertions to standard error via cerr are not buffered by default because the ios::unitbuf bit is on. The default behavior can be changed with the ios::setf() public member function, or with the setiosflags() manipulator.
Insertions to standard error via clog are buffered by default because the ios::unitbuf bit is not on. The default behavior can be changed with the ios::setf() public member function, or with the setiosflags() manipulator.
istream input can be either formatted or unformatted.
The istream operator >>() function is used to read formatted values from a stream. It's called an extractor. Characters are read and interpreted according to the type of object being extracted.
All operator >>() functions perform the same basic sequence of operations:
The operator >>() functions return a reference to the specified stream, so that multiple extractions can be done in one statement.
Errors are indicated via ios::iostate. The ios::failbit bit is set if the characters read from the stream could not be interpreted for the required type. The ios::badbit bit is set if the extraction of characters from the stream failed in such a way as to make subsequent extractions impossible. The ios::eofbit bit is set if the stream was located at the end when the extraction was attempted.
The unformatted input functions are used to read characters from the stream, without interpretation.
Like the extractors, the unformatted input functions follow a pattern:
Errors are indicated via the ios::iostate bits. The ios::failbit bit is set if the extraction of characters from the stream failed. The ios::eofbit bit is set if the stream was located at the end of input when the operation was attempted.
Output can also be formatted or unformatted.
The ostream operator <<() function is used to write formatted values to a stream. It's called an inserter. Values are formatted and written according to the type of object being inserted and ios::fmtflags.
All operator << functions perform the same basic sequence of operations:
The operator << functions return a reference to the specified stream, so that multiple insertions can be done in one statement.
Errors are indicated via ios::iostate. The ios::failbit bit is set if the operator << function fails while writing the characters to the stream.
The unformatted output functions are used to write characters to the stream without conversion.
Like the inserters, the unformatted output functions follow a pattern:
Errors are indicated via ios::iostate. The ios::failbit bit is set if the function fails while writing the characters to the stream.
Each of the classes and functions in the C++ Class Library is described in this reference. Each description consists of a number of subsections:
For class member functions, the protection associated with the function is indicated via the presence of one of the private, protected or public keywords.
The full function prototype is specified. Virtual class member functions are indicated via the presence of the virtual keyword in the function prototype.