Class O_Stream

Class O_Stream

Placement in the overall structure

Module
object
Base classes
Stringbuffer
Derived classes
CGA_Stream
Time of creation
Task 1
Files
o_stream.h o_stream.cc

Description

The functionality of the O_Stream class is essentially the same as that of the ostream class of the well-known C++ IO Streams library.

The O_Stream class defines the operator<< operator for several pre-defined data types and thereby realizes output functionality similar to C++'s iostream library. By default, this class supports printing characters, strings and integer numbers of various bit sizes. Another operator<< operator allows to use so-called 'manipulators'.

It should be possible to choose between the decimal, binary, octal and hexadecimal systems for the representation of integer numbers. Please note the usual representation of negative numbers: In the decimal system with leading minus sign, in the octal and hexadecimal system without minus sign, but exactly as they appear in the machine word. (Intel CPUs use internally the 2's complement for negative numbers. -1 is hexadecimal therefore FFFFFFFF and octal 377777777.)

Public methods

Each of the following public methods returns a reference to its own O_Stream object. This makes it possible to use multiple of the operators in one expression,
e.g. kout << "a = " << a;

Append the character c to the collected characters.
  • O_Stream& operator<< (unsigned char c)
  • O_Stream& operator<< (char c)
Append the number in the selected numeral system.
  • O_Stream& operator<< (unsigned short number)
  • O_Stream& operator<< (short number)
  • O_Stream& operator<< (unsigned int number)
  • O_Stream& operator<< (int number)
  • O_Stream& operator<< (unsigned long number)
  • O_Stream& operator<< (long number)
Append the pointer in hexadecimal system.
  • O_Stream& operator<< (void* pointer)
Append the null-terminated string text (without the null termination).
  • O_Stream& operator<< (char* text)
Calling the manipulator function fkt.
  • O_Stream& operator<< (O_Stream& (*fkt) (O_Stream&))

Manipulators

In order to be able to conveniently select the numeral system and insert line breaks when formatting text with the help of the O_Stream class, so-called manipulators should be defined. The expression
kout << "a = " << dec << a << " ist hexadezimal " << hex << a << endl;
should then, for example, format the value of the variable a first in decimal and then in hexadecimal notation, add a newline at the end and flush the stream.

The desired properties can be realized if hex, dec, oct, bin, and endl are defined as functions (i.e., not as methods of the O_Stream class), each of which receives and returns a reference to an O_Stream object. By this signature the already named operator O_Stream& O_Stream::operator<< (O_Stream& (*fkt) (O_Stream&)) is selected with the expression mentioned, which must execute then only the function specified as parameter.

In total, the following manipulators should be defined:

O_Stream& endl (O_Stream& os);
inserts a line break and calls os.flush().
O_Stream& bin (O_Stream& os);
selects the binary number system.
O_Stream& oct (O_Stream& os);
selects the octal number system.
O_Stream& dec (O_Stream& os);
selects the decimal number system.
O_Stream& hex (O_Stream& os);
selects the hexadecimal number system.

Comments

The manipulator term was taken from the book "Bjarne Stroustrup: The C++ Programming Language". You can find further explanations there.