Breaking News

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion operator << . Thus, the following statement sends the character ‘x’ to the stream cout:
cout << `x’;
Extraction is the operation of taking characters from a stream, expressed by the overloaded extraction operator >>. The following expression (where ch has type char) obtains a single character from the stream cin and stores it in ch.
cin >> ch;
Although streams produce or consume characters, insertion and extraction can be used with other types of data.
For instance, in the following statements, the characters ‘1’ , ‘2’ , and ‘3’ are inserted into the stream cout, after which characters are extracted from cin, interpreted as an integer, and the result is assigned to i:
int i = 123;
cout << i;
cin >> i;
Insertion and extraction can be overloaded for user-defined types as well. Consider the following code:
class fraction
{
int numer;
unsigned denom;
friend ostream& operator <<(ostream& os, fraction& f)
{
return os << f.numer << `/’ << f.denom;
};
};
These statements define an insertion operator for a user-defined fraction class, which can be used as conveniently and easily as insertion of characters or ints.
Get and put pointers
The definition of C++ stream I/O makes use of the concepts of the get pointer and the put pointer. The get pointer for a stream indicates the position in the stream from which characters are extracted.
Similarly, the put pointer for a stream indicates the position in the stream where characters are inserted. Use of the insertion or extraction operator on a stream causes the appropriate pointer to move.
Note that these are abstract pointers, referencing positions in the abstract sequence of characters associated with the stream, not C++ pointers addressing specific memory locations.
You can use member functions of the various stream classes to move the get or put pointer without performing an extraction or insertion.
For example, the fstream::seekoff() member function moves the get and put pointers for an fstream.
The exact behavior of the get and put pointers for a stream depends on the type of stream.
For example, for fstream objects, the get and put pointers are tied together.
That is, any operation that moves one always moves the other. For strstream objects, the pointers are independent. That is, either pointer can be moved without affecting the other.
The get and put pointers reference positions between the characters of the stream, not the characters themselves.
For example, consider the following sequence of characters as a stream, with the positions of the get and put pointers as marked in Illustration of Get and Put Pointers:
Illustration of Get and Put Pointers
In this example, the next character extracted from the stream is ‘c’, and the next character inserted into the stream replaces the ‘r’.

Check Also

Virtual Functions In C++

  Virtual function is a member function of a base class and redefined by a …