Overloading Insertion Operator (<<)

Written by Raza on. Posted in C++

We have been discussing rather overloading various operators in the object oriented paradigm in the past. Overloading operators in our class is solely for our own benefit and effectiveness of the program. It saves us from writing lengthy complicated code over and over again. When we work with objects and there is an Array inside that object, we would certainly want to output it time and again. Instead of making a loop for that every time, we overload the insertion operator (<<) once and for all which is used to cout the data on our screen. Let us see how to do that. 

We are working yet again with our Vector class with an integer array inside that. If we want to output the array of an object, we would write a code like this.

<p>for (int i = 0 ; i &lt; size ; ++i)</p>
<p>.  cout &lt;&lt; arr [i] ;</p>


If we are outputting our array ten times in our program, we would have to write this loop simply ten times. So we write this loop only once in our overloaded function for the insertion operator (<<). It is known as insertion operator as it inserts user data into the stream to be output. After overloading it, we can simply write the following syntax for outputting the array.

<p>cout &lt;&lt; v1 ;</p>


Let me give you some basic info on what actually cout is. Cout is the standard output stream. It is an object of ostream or output stream class. Ostream class is declared in the iostream library and access iostream library using the file std of namespace type. We use the insertion operator (<<) to write our data to the output stream.

Keep this thing in mind that the overloaded function for insertion operator for output in our program has to be a non-member function of the class. Why? Search for this yourself. When we make this function non-member, we cannot directly access the array inside it which is a private data member of our class. In order to access that, we would have to make either getter function or overload the indexing operator ([]) to access it.

We have done overloading of indexing operator ([]) in one of our posts so we would be using that right now. Our overloaded function requires two things. One is the object whose data is to be output and the other is the standard output stream on which to write the data and print on the screen. In short, we pass our Vector object and the cout object as arguments to it. It outputs the whole array and returns the reference to the stream object. The following code would be sufficient for that .

<p>ostream&amp; operator &lt;&lt; (ostream &amp;cout, Vector &amp;obj)<br />
{<br />
.  for (int i = 0 ; i &lt; obj.getsize() ; ++i)<br />
.   cout &lt;&lt; obj[i] ;<br />
.  cout &lt;&lt; endl ;<br />
.  return cout ;<br />


Some people make this function a friend function. Friends are good to have but its better off without them in real life as well. They can access the private data members of the class which is against the principle of information hiding. Thus it is better to declare them as simple non-member and not as friend functions. Like insertion operator, we can also overload insertion operator and take the input directly in the object. We will see that the next time.

Tags: , , , ,