1

so I am coding my c++ homework assignment and there is a final part where he wants us to Replace the formatted output method (toString) with an overloaded output/insertion operator. TO be 100% honest I have no idea what he means by this. I've searched around a bit and found example codes using an overloaded insertion operator, but can't seem to find how to incorporate it into my code. Though I think I may be looking in the wrong place. My toString is as follows:

string Movie::toString() const {
    ostringstream oS;
    oS << "\n\n====================== Movie Information\n"
    << "\n             Movie Title:\t" << title << "  (" << releaseYear << ")"
    << "\n    US Rank & Box Office:\t" << usRank << "\t$" << usBoxOffice
    << "\nNon-US Rank & Box Office:\t" << nonUSRank << "\t$" << nonUSBoxOffice
    << "\n World Rank & Box Office:\t" << worldRank << "\t$" << worldBoxOffice
    << "\n";
    return oS.str();
}

Like I mentioned I'm not sure what "overloaded" means, so If for some reason this isn't enough information for you to help me with the problem directly, then can you give me a brief description of what he may mean by replacing the current output with an overloaded output operator. Thank You

edit: This is the next question I have. https://stackoverflow.com/questions/14924621/c-overloaded-output-operator-cont

Community
  • 1
  • 1
Jason Schayer
  • 171
  • 2
  • 5
  • 15

2 Answers2

6

To overload a function means to provide other functions with the same name but different parameter types. Operators can also be overloaded. Many operators have a corresponding function that can be overloaded called operator??, where ?? is the operator itself. For example if you have two objects x and y of class type T, you could overload operator+. Overloading an operator allows you to give some meaning to using that operator with the type. So now you could do x + y.

The stream insertion operator is <<. It's what you use when you do std::cin << "hello"; - it inserts into the stream. This operator can also be overloaded, just as + was overloaded above. The function you need to overload is called operator<<.

There are two ways to overload a binary operator like << (binary because it takes two operands, one on the left side and one on the right, left << right). One is to make it a member of the type of left and give it a single parameter of the type of right. The other is to make it a non-member function with two parameters, one the type of left and the other the type of right. Since the type of your left will be std::ostream, you can't modify the class (because it's provided by the standard), so you'll have to go with option two.

So your free function needs to look something like this:

std::ostream& operator<<(std::ostream& os, const Movie& movie) {
  // Insert everything you want into `os`
  return os;
}

Now this function will be called whenever you do << with an std::ostream on the left and a Movie on the right.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • I understand how the operator is overloaded now, and ways it can be used, but still not completely sure why you use it. Thank you for the answer. I will study on this more. – Jason Schayer Feb 17 '13 at 18:02
  • Though I do have another question if your up for it. I also have a getMovie function where it picks out a movie using and index. Would I overload the function the same way we did with tostring. – Jason Schayer Feb 17 '13 at 18:16
  • @JasonSchayer You use it because you want to be able to do `Movie m; std::cout << m;`. This will print out the contents of the movie `m` in the form you specify. Just like doing `std::cout << 10;` will print out the `int` 10. Now you can print out a `Movie`! – Joseph Mansfield Feb 17 '13 at 18:19
  • @JasonSchayer If your new question is for clarification, ask it here, sure. If it's separate, post a new question on SO. – Joseph Mansfield Feb 17 '13 at 18:20
  • Ill post it in another SO just to make things easier and link it here – Jason Schayer Feb 17 '13 at 18:28
3

I think your task is meant to be writing an overloaded operator << which allows you to write the string representation of your object to an output stream:

std::ostream& operator <<(std::ostream& os, const Movie& movie) {
    os << "\n\n====================== Movie Information\n"
       << "\n             Movie Title:\t" << movie.title << "  (" << movie.releaseYear << ")"
       << "\n    US Rank & Box Office:\t" << movie.usRank << "\t$" << movie.usBoxOffice
       << "\nNon-US Rank & Box Office:\t" << movie.nonUSRank << "\t$" << movie.nonUSBoxOffice
       << "\n World Rank & Box Office:\t" << movie.worldRank << "\t$" << movie.worldBoxOffice
       << "\n";
    return os;
}

And you use this operator as you could do with built-in types:

Movie m;

// Do something with m

cout << m;      // Write m to the standard output
buc
  • 6,268
  • 1
  • 34
  • 51