Overloading Extraction Operator (>>)

Written by Raza on. Posted in C++

The extraction operator is used to take input directly in objects. In Object Oriented Programming, we want everything to be as simple and close to built in data types as is possible. The concept of overloading various operators is one facet of this need. For input and output, we especially want ease and smooth syntax to flow throughout the program.We saw how to overload insertion operator for taking output in our program. This time we shall see how to overload the extraction operator (>>) for taking input directly in an object.  

Function of Extraction Operator:

We are working with the same Vector class with which we are quite familiar by now.We have overloaded the indexing operator to access any particular index of the array residing in our object. Normally for an array, we take input by iterating through a loop. If we take input ten times in our program, we have to write the loop ten times. For simplicity, we overload extraction operator which takes input at the cost of one line syntax.

In C++, input and output is not a built-in functionality. We take input using iostream library by including it at the top of our program. cin is the object of istream class that takes input using extraction operator (>>). cin is used as the standard input stream. The syntax of overloading the extraction operator is also pretty much the same as the insertion operator with some minor differences.

To the extraction operator’s overloaded function, we pass the object by reference and return the reference of the object of istream class. First we have to take input the size of the array from user. Then we allocate that amount of memory to the array and then take input into the array itself. If the data member of size is private which it should be, (read efficient programming techniques) we would have to make a setter for the size as well. Then for allocating memory, a setter for the array would also have to be made.


Example Source Code for Extraction Operator:

The complete code would look like the following one.

<p>class Vector<br />
{<br />
.  private :<br />
.   int *arr ;<br />
.   int size ;<br />
.  public :<br />
.  void setsize (const int i)<br />
.  {<br />
.   size = i ;<br />
.  }<br />
.  void setarr(const int i)<br />
.  {<br />
.   arr = new int [i] ;<br />
.  }<br />
.  Vector ()<br />
.  {<br />
.   size = 0 ;<br />
.   arr = NULL ;<br />
.  }<br />
.  int getsize () const<br />
.  {<br />
.  return size ;<br />
.  }<br />
.  int&amp; operator [] (const int i)<br />
.  {<br />
.   return arr[i] ;<br />
.  }<br />
.  Vector&amp; operator = (const Vector &amp;) ;</p>
<p>} ;</p>
<p>istream&amp; operator &gt;&gt; (istream &amp;cin, Vector &amp;obj)<br />
{<br />
.  int size = 0 ;<br />
.  cout &lt;&lt; "Please Enter the size of the array" &lt;&lt; endl ;<br />
.  cin &gt;&gt; size ;<br />
.  obj.setsize(size) ;<br />
.  obj.setarr(obj.getsize()) ;<br />
.  for (int i = 0 ; i &lt; obj.getsize() ; ++i)<br />
.   cin &gt;&gt; obj[i] ;<br />
.  cout &lt;&lt; endl ;<br />
.  return cin ;<br />
<p>int main ()<br />
{<br />
Vector v1 ;<br />
cin &gt;&gt; v1 ;<br />
cout &lt;&lt; v1 ;<br />


Now, the extraction operator can be used to input any sort of data we requir in accordance with our class. This saves a lot of hassle and improves our code’s efficiency and readability. We should always look to overload the extraction operator whenever dealing with classes in our C++ codes.

Tags: , , , ,