Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

(C++) Not sure what the problem wants me to do.

  1. May 10, 2010 #1
    I have this HW problem, but I cannot figure out what it wants me to do. I don't want anyone to solve it for me, just point me in the right direction.

    Suppose that a class hierarchy has a virtual method print
    Code (Text):

    class C {
    virtual void print( ostream& ) const;
    that outputs C's members to the stream ostream. Write one overload of << so that the statement

    Code (Text):
    out << p;
    outputs p's members to the stream out, where p is any object that belongs to a class in the hierarchy.

    What I don't understand is the out << p bit. Am I supposed to be able to do

    Code (Text):
    out << p;
    in my main() and have the overload call p.print() ?
  2. jcsd
  3. May 10, 2010 #2

    D H

    User Avatar
    Insights Author

    Staff: Mentor

    That is exactly what you are supposed to do.

    You should probably make your overloaded function obey the semantics of the << operator as used with streams so that given two such objects p and q the following works:
    Code (Text):
    out << "Members of p:\n" << p << "\nMembers of q:\n" << q;
    Last edited: May 10, 2010
  4. May 10, 2010 #3


    User Avatar
    Science Advisor
    Homework Helper

    Indeed. So they want you to override operator<<
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Similar Discussions: (C++) Not sure what the problem wants me to do.
  1. C++ problem (Replies: 3)

  2. Wanted help in c++ (Replies: 2)