Jason Schayer
Jason Schayer

Reputation: 171

C++ overloaded output operator

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

Upvotes: 1

Views: 4346

Answers (2)

buc
buc

Reputation: 6358

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

Upvotes: 3

Joseph Mansfield
Joseph Mansfield

Reputation: 110668

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.

Upvotes: 6

Related Questions