Class Stringbuffer
Placement in the overall structure
- Module
- object
- Base classes
- None
- Derived classes
- O_Stream
- Time of creation
- Task 1
- Files
strbuf.h strbuf.cc
Description
The Stringbuffer class provides a buffer for collecting characters to be printed to an output device, in our case the PC screen. The actual output occurs once the buffer is full, or when the user explicitly calls flush(). As Stringbuffer is intended to be device independent, flush() is a (pure) virtual method that must be defined by derived classes.
Public methods
void put (char c)
- Inserts a character c into the buffer. If the buffer is
full afterwards, it gets cleared by calling
flush ()
. virtual void flush()=0
- Method to output the buffer contents. This pure-virtual method must be defined in derived classes, as only they "know" how to do the actual output. flush() also resets the position indicator pos.
Implementation notes
For buffering the characters a fixed dimensioned field is appropriate, which the derived classes must be able to access. Also the number of characters and the last written index should be known in the specialized flush() methods.
Comment
The reason for the introduction of this class was the realization that outputs of a program very often consist of many small components, for example, when the names and contents of variables are to be displayed. A few longer texts, in contrast, can usually be output much more efficiently than many short ones. Therefore it seems to make sense to join the individual components with the help of a Stringbuffer object before the output and to display them collectively later, e.g. at a line break.