{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Session_12 - Overloading the < Operator One of the more...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Overloading the << Operator One of the more useful features of classes is that you can overload the << operator so that you can use it with cout to display an object’s contents. Recall our Vector class. Suppose we have a Vector V. To display Vector values, we’ve been using show_vector() . It would be more convenient (and consistent) to say cout << V; You can do this because << is one of the C++ operators that can be overloaded. In fact, it is already heavily overloaded. In its basic form, it’s already overloaded because << also is used to shift bits left (as seen at the beginning of the previous session). Within iostream there exists a class called ostream (output stream). The ostream class overloads the << operator, making it useful with a variety of data types. Cout is an ostream object that is smart enough to recognize all the basic C++ types. This is because the ostream class declaration includes an overloaded operator<<() definition for each of the basic types. That is, one definition uses an int argument, another uses a double argument, etc. So one way to “teach” cout to recognize a Vector object is to add a new function operator definition to the ostream class declaration. This is not recommended; it is better to teach the Vector class to use cout. First Version of Overloading To teach the Vector class to use cout, you’ll need a friend function. Why? Because cout << V; uses two objects; one with the ostream class object(cout) and the other with a Vector object. If you use a Vector member function to overload <<, the Vector object would have to come first (remember, left hand side?) as it does with the overloaded *, + and -. In other words, V << cout; The above syntax is confusing and not consistent. By using a friend function, you can overload the operator this way: void operator<<(ostream & os, const Vector & V) { os << “(x,y) = (“ << V.x << “,” << V.y << “)”; } The prototype for our new overloaded function would look like: friend void operator<<(ostream & os, const Vector &v); Note that the new operator <<() uses an ostream reference os as its first argument. Page 1 of 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Second Version of Overloaded << But the implementation has a problem. Statements such as cout << V; work fine, but how about cout << “The value of V is “ << V << endl; To understand why this doesn’t work and what must be done to make it work, you first need to understand how cout operates. Consider the following code: int x = 5; int y = 8; cout << x << y; C++ reads the output statement from left to right, meaning it is equivalent to the following: (cout << x) << y; The << operator, as defined in iostream, takes an ostream object as its left hand side. Clearly, cout << x; satisfies that requirement because cout is an ostream object. But the output statement also requires that the whole expression cout << x be a type ostream object, because that expression is to the left of << y Therefore, the ostream class implements the operator<<() function so that it returns an ostream object. In particular, it returns the invoking object—cout in this case. Thus the expression (cout <<x) is itself an ostream object, and it can be used to the left of the << operator.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}