View Full Version : C++ Overloaded operators

09-18-01, 07:29 PM
I am trying to learn this language and I found an old lab manual of a friend and the part I am on now is talking about overloaded operators. I am slightly confused and wondering if someone would shed a little light on this if they can. Here it is:

The lab says to write a function Print() with signature (ostream &) and return type void to output a Rock object. They say to put the function heading in both the prototype and the definition. Is it suppost to look like this. O yeah, they say to make it a const.
void Print(ostream &) const;
They then want the overloaded operator<<() to be used. This code looks like this
ostream & operator<<(ostream & out, Rock rockVal)
rockVal.Print(out);// I am guessing here, they just said to use statements to output the characters corresponding to rockVal
return out;

Now how do I encorporate these two together? When they say that void Print(ostream &) const; should be in both the prototype and definition. Can someone explain what they're talking about.

09-19-01, 01:09 PM
Okay, this is a little confusing without knowing what the class Rock is, or how to print it. So, I'll be a little vague about the inner workings of the methods (a.k.a member functions). Also, I'm assuming that the Print method is part of the class Rock, and the overloaded operators are not class members (that is to say, they are used in the scope of the main() namespace--this is made clear because of the use of two parameters, namely ostream and Rock, being passed in addition to the implied functionality desired).

Dealing with the Print prototype portion first, you have something resembling this:

class Rock {
// some variables

// some methods

void Print(ostream &) const;


Next, you have the definition of Print:

void Rock::Print (ostream & value) const {

// code to handle this method here...


Now on to the operators, the instructions are a little vague. I assume they want you to overload the existing operators that are predefined to ostream. So, the prototype and definition would be similar to this (I'm including the use as well):

#include <iostream>

// Rock class here, or include it here

ostream & operator<<(ostream &, Rock);
ostream & operator<<(ostream & out, Rock rockVal){

// This code works, so I'll stick with it--less confusing that way!
return out;


int main (int argc, char *argv[]){

Rock my_rock;

// Use the overloaded <<
cout << my_rock << ends;


More likely than not, you would rather have the operator<< as a member of the class Rock--unlike the instructions are doing it.

09-19-01, 02:37 PM
Let me just throw this out. This is the part you are suppost to uncomment to test to see if it works.
/********* BEGIN PART 3 **********/
cout << "\n===========================================\n"
" PART 3: Testing function member Print()"
cout << "Name of\n"
<< " sample is....";
cout << endl;
cout << " rockVal is...";
cout << endl;

Now where would you stick in the ostream & operator<<(etc..) in that. I looks like you just uncomment that without having to add anything else.

Thanks again for your time Stu

09-20-01, 12:49 AM
Okay, I assume that is there to see if your Print method works correctly. But, to transform that code to use the overloaded operator<< it would look like this (assuming you added the code in that I mentioned in my previous post):

cout << "\n===========================================\n"
<< " PART 3: Testing function member Print()"
<< "\n===========================================\n"
<< "Name of\n sample is...."
<< sample
<< "\nrockVal is..."
<< rockVal
<< '\n'
<< ends;

I cleaned it up a little. I used only one "call" to the ostream, in effect concatenating everything into one character ostream. In addition, I lead off every new line with << (some compilers will throw errors if you don't do this--it's a portability thing that I got into the habit of doing). Also, both ends and endl flush the ostream's buffer to standard output. Repeated flushing, in this case, is not necessary. One other thing to note, on some systems endl behaves the same as ends--in that it only flushes the ostream's buffer to standard output, but does not add a newline (another portability thing--using << '\n' << ends; instead of << endl; ).

09-20-01, 10:03 AM
Hey thanks Stu, what do you this for a living or something? Good for us that you go to this board. Thanks again.

09-20-01, 10:02 PM
Your welcome, and yes, I do this for a living. :)