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: { 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 DocumentRight 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,
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.

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 9

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online