Overloading Problem

Could someone please give me some help on how to write a member function to overload the post and pre ++ and --? I can't figure out how to do it! HELP!

Comments

  • : Could someone please give me some help on how to write a member function to overload the post and pre ++ and --? I can't figure out how to do it! HELP!
    :

    This should do the job for you

    Zeke

    [code]

    // demo to overload ++ (prefix and postfix)
    /////////////////////////////////////////////////

    #include
    using namespace std;

    class rectangle
    { public:
    int length, width;
    rectangle() {length = width = 0;}
    rectangle operator++(); //prefix version;
    rectangle operator++(int dummy);
    };

    void main()
    { rectangle myRectangle;
    myRectangle.length = 5;
    myRectangle.width = 10;
    cout << "size = " << myRectangle.length << " x " << myRectangle.width << endl;
    ++myRectangle;
    cout << "size = " << myRectangle.length << " x " << myRectangle.width << endl;
    myRectangle++;
    cout << "size = " << myRectangle.length << " x " << myRectangle.width << endl;
    }

    rectangle rectangle::operator ++()
    // prefix version
    { length ++;
    width ++;
    return *this;
    }

    rectangle rectangle::operator ++(int dummy)
    // postfix version
    { rectangle temp = *this; // hold orig values
    length++;
    width++;
    return temp; //return orig value;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion