
lafore_robert_objectoriented_programming_in_c
.pdf

Chapter 11
566
10.Carry out the modification, discussed in Exercise 7, to the PARSE program of Chapter 10. That is, make it possible to parse expressions containing floating-point numbers. Combine the classes from Exercise 7 with the algorithms from PARSE. You’ll need to operate on pointers to tokens instead of characters. This involves statements of the kind
Number* ptrN = new Number(ans); s.push(ptrN);
and
Operator* ptrO = new Operator(ch); s.push(ptrO);




Chapter 12
570
As you can see from Figure 12.1, the ios class is the base class for the hierarchy. It contains many constants and member functions common to input and output operations of all kinds. Some of these, such as the showpoint and fixed formatting flags, we’ve seen already. The ios class also contains a pointer to the streambuf class, which contains the actual memory buffer into which data is read or written, and the low-level routines for handling this data. Ordinarily you don’t need to worry about the streambuf class, which is referenced automatically by other classes.
The istream and ostream classes are derived from ios and are dedicated to input and output, respectively. The istream class contains such functions as get(), getline(), read(), and the overloaded extraction (>>) operators, while ostream contains put() and write(), and the overloaded insertion (<<) operators.
The iostream class is derived from both istream and ostream by multiple inheritance. Classes derived from it can be used with devices, such as disk files, that may be opened for both input and output at the same time. Three classes—istream_withassign, ostream_withassign, and iostream_withassign—are inherited from istream, ostream, and iostream, respectively. They add assignment operators to these classes.
The following summary of stream classes may seem rather abstract. You may want to skim it now, and return to it later when you need to know how to perform a particular stream-related activity.
The ios Class
The ios class is the granddaddy of all the stream classes, and contains the majority of the features you need to operate C++ streams. The three most important features are the formatting flags, the error-status flags, and the file operation mode. We’ll look at formatting flags and error-status flags next. We’ll save the file operations mode for later, when we talk about disk files.
Formatting Flags
Formatting flags are a set of enum definitions in ios. They act as on/off switches that specify choices for various aspects of input and output format and operation. We won’t provide a detailed discussion of each flag, since we’ve already seen some of them in use, and others are more or less self-explanatory. Some we’ll discuss later in this chapter. Table 12.1 is a complete list of the formatting flags.


Chapter 12
572
We’ve also used the setiosflags() manipulator (see the SALEMON program in Chapter 7, “Arrays and Strings”):
cout << setiosflags(ios::fixed) |
// use fixed decimal point |
<<setiosflags(ios::showpoint) // always show decimal point
<<var;
As these examples demonstrate, manipulators come in two flavors: those that take an argument and those that don’t. Table 12.2 summarizes the important no-argument manipulators.
TABLE 12.2 No-Argument ios Manipulators
Manipulator |
Purpose |
ws |
Turn on whitespace skipping on input |
dec |
Convert to decimal |
oct |
Convert to octal |
hex |
Convert to hexadecimal |
endl |
Insert newline and flush the output stream |
ends |
Insert null character to terminate an output string |
flush |
Flush the output stream |
lock |
Lock file handle |
unlock |
Unlock file handle |
|
|
You insert these manipulators directly into the stream. For example, to output var in hexadecimal format, you can say
cout << hex << var;
Note that manipulators affect only the data that follows them in the stream, not the data that precedes them. Table 12.3 summarizes the important manipulators that take arguments. You need the IOMANIP header file for these functions.
TABLE 12.3 ios Manipulators with Arguments
Manipulator |
Argument |
Purpose |
setw() |
field width (int) |
Set field width for output |
setfill() |
fill character (int) |
Set fill character for output |
|
|
(default is a space) |
setprecision() |
precision (int) |
Set precision (number of digits |
|
|
displayed) |
setiosflags() |
formatting flags (long) |
Set specified flags |
resetiosflags() |
formatting flags (long) |
Clear specified flags |
|
|
|


Chapter 12
574
TABLE 12.5 Two-Argument Version of setf()
First Argument: Flags to Set |
Second Argument: Field to Clear |
dec, oct, hex |
basefield |
left, right, internal |
adjustfield |
scientific, fixed |
floatfield |
|
|
By using the techniques shown here with the formatting flags, you can usually figure out a way to format I/O not only for the keyboard and display, but, as we’ll see later in this chapter, for files as well.
The istream Class
The istream class, which is derived from ios, performs input-specific activities, or extraction. It’s easy to confuse extraction and the related output activity, insertion. Figure 12.2 emphasizes the difference.
FIGURE 12.2
File input and output.
Table 12.6 lists the functions you’ll most commonly use from the istream class.
TABLE 12.6 istream Functions
Function |
Purpose |
>> |
Formatted extraction for all basic (and overloaded) types. |
get(ch); |
Extract one character into ch. |
get(str) |
Extract characters into array str, until ‘\n’. |