Results 1 to 6 of 6

Thread: C++ Overloaded operators

  1. #1

    C++ Overloaded operators

    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.
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  2. #2
    Regular Member
    Join Date
    Aug 1999
    Posts
    341
    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:

    Code:
    class Rock {
      // some variables
    
     // some methods
    
     void Print(ostream &) const;
    
    };
    Next, you have the definition of Print:

    Code:
    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):

    Code:
    #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!
      rockVal.Print(out);
      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.
    Last edited by Stu; 09-19-01 at 02:10 PM.

  3. #3

    Wow

    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()"
    "\n===========================================\n";
    cout << "Name of\n"
    << " sample is....";
    sample.Print(cout);
    cout << endl;
    cout << " rockVal is...";
    rockVal.Print(cout);
    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
    Last edited by Orphious; 09-19-01 at 03:48 PM.
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  4. #4
    Regular Member
    Join Date
    Aug 1999
    Posts
    341
    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):

    Code:
    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; ).
    Last edited by Stu; 09-20-01 at 01:57 AM.

  5. #5

    great

    Hey thanks Stu, what do you this for a living or something? Good for us that you go to this board. Thanks again.
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  6. #6
    Regular Member
    Join Date
    Aug 1999
    Posts
    341
    Your welcome, and yes, I do this for a living.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •