15.1. Introduction
The C++ standard libraries provide an
extensive set of input/output capabilities. This chapter discusses a range of
capabilities sufficient for performing most common I/O operations and overviews
the remaining capabilities. We discussed some of these features earlier in the
text; now we provide a more complete treatment. Many of the I/O features that
we'll discuss are object oriented. This style of I/O makes use of other C++
features, such as references, function overloading and operator overloading.
C++ uses type-safe I/O. Each I/O
operation is executed in a manner sensitive to the data type. If an I/O member
function has been defined to handle a particular data type, then that member
function is called to handle that data type. If there is no match between the
type of the actual data and a function for handling that data type, the compiler
generates an error. Thus, improper data cannot "sneak" through the system (as
can occur in C, allowing for some subtle and bizarre errors).
Users can specify how to perform I/O for
objects of user-defined types by overloading the stream insertion operator
(<<) and the stream extraction
operator (>>). This extensibility is one
of C++'s most valuable features.
Software Engineering Observation 15.1
|
Use the C++-style I/O exclusively in
C++ programs, even though C-style I/O is available to C++
programmers. |
Error-Prevention Tip 15.1
|
C++ I/O is type
safe. |
Software Engineering Observation 15.2
|
C++ enables a common treatment of I/O for
predefined types and user-defined types. This commonality facilitates software
development and reuse. |