Share via


ostream

#include <iostream.h>

The ostream class provides the basic capability for sequential and random-access output. An ostream object has a streambuf-derived object attached, and the two classes work together; the ostream class does the formatting, and the streambuf class does the low-level buffered output.

You can use ostream objects for sequential disk output if you first construct an appropriate filebuf object. (The filebuf class is derived from streambuf.) More often, you will use the predefined stream objects cout, cerr, and clog (actually objects of class ostream_withassign), or you will use objects of classes ofstream (disk file streams) and ostrstream (string streams).

All of the ostream member functions write unformatted data; formatted output is handled by the insertion operators.

Derivation

It is not always necessary to derive from ostream to add functionality to a stream; consider deriving from streambuf instead, as illustrated in Deriving Your Own Stream Classes. The ofstream and ostrstream classes are examples of ostream-derived classes that construct member objects of predetermined derived streambuf classes. You can add manipulators without deriving a new class.

If you add new insertion operators for a derived ostream class, then the rules of C++ dictate that you must reimplement all the base class insertion operators. If, however, you reimplement the operators through inline equivalence, no extra code will be generated.

Construction/Destruction — Public Members

ostream

Constructs an ostream object that is attached to an existing streambuf object.

~ostream

Destroys an ostream object.

Prefix/Suffix Functions — Public Members

opfx

Output prefix function, called prior to insertion operations to check for error conditions, and so forth.

osfx

Output suffix function, called after insertion operations; flushes the stream’s buffer if it is unit buffered.

Unformatted Output — Public Members

put

Inserts a single byte into the stream.

write

Inserts a series of bytes into the stream.

Other Functions — Public Members

flush

Flushes the buffer associated with this stream.

seekp

Changes the stream’s put pointer.

tellp

Gets the value of the stream’s put pointer.

Operators — Public Members

operator <<

Insertion operator for various types.

Manipulators

endl

Inserts a newline sequence and flushes the buffer.

ends

Inserts a null character to terminate a string.

flush

Flushes the stream’s buffer.

Output Stream Classes

See Also   streambuf, ofstream, ostrstream, cout, cerr, clog

Example

class xstream : public ostream
{
public:
    // Constructors, etc.
    // ........
    inline xstream& operator << ( char ch ) // insertion for char
    {
          return (xstream&)ostream::operator << ( ch );
    }
    // ........
    // Insertions for other types
};