Assume the existence of a Window class with integer data members width and height. Overload the << operator for the Window class -- i.e., write a nonmember ostream-returning function that accepts a reference to an ostream object and a constant reference to a Window object and sends the following to the ostream: 'a (width x height) window' (without the quotes and with width and height replaced by the actual width and height of the window. Thus for example, if the window had width=80 and height=20, << would send 'a (80 x 20) window' to the ostream.) Don't forget to have the function return the proper value as well. Assume the operator has been declared a friend in the Window class .

LANGUAGE: C++

CHALLENGE:

Assume the existence of a Window class with integer data members width and height. Overload the << operator for the Window class — i.e., write a nonmember ostream-returning function that accepts a reference to an ostream object and a constant reference to a Window object and sends the following to the ostream: ‘a (width x height) window’ (without the quotes and with width and height replaced by the actual width and height of the window. Thus for example, if the window had width=80 and height=20, << would send ‘a (80 x 20) window’ to the ostream.) Don’t forget to have the function return the proper value as well. Assume the operator has been declared a friend in the Window class.

SOLUTION:

The Bad News: The content you are trying to access is only available to members.

The Good News: You too can be a member, simply Click Here.

Already a member?

Posted in ,

3 Comments

  1. booga on October 12, 2014 at 6:17 pm

    friend ostream& operator << (ostream& stm, Window& w)
    {
    return stm << "a ("<<w.width << " x " << w.height << ") window";
    }

  2. colby on June 15, 2018 at 5:31 pm

    try this

    friend istream& operator >>(istream& ins, Window& obj)
    {
    int w, h;
    ins >>w;
    ins >>h;
    obj.height=h;
    obj.width=w;
    return ins;
    }

  3. colby on June 15, 2018 at 5:31 pm

    friend istream& operator >>(istream& ins, Window& obj)
    {
    int w, h;
    ins >>w;
    ins >>h;
    obj.height=h;
    obj.width=w;
    return ins;
    }

Leave a Comment

You must be logged in to post a comment.