Question

I've viewed multiple topics on stackoverflow and I'm not getting anywhere with this class assignment. I believe I'm using the code as it was presented in the book but I'm having problems with the = operator not copying and the - operator returning two values concatenated. I'm hoping you can help me understand and point me in the right direction on this. Any help is appreciated.

The rectangleType class has two protected members, length and width, and a function called rectangleType.area() that multiplies them. In the assignment, I'm supposed to change book code that returns length and width and return area instead but I can't get these to work right. (Relational and stream operators are working fine.)

From rectangleType.h:

rectangleType operator=(const rectangleType&) const; // replace one rectangle with another
rectangleType operator-(const rectangleType&) const; // subtract one rectangle from another

From rectangleTypeImp.cpp

rectangleType rectangleType::operator=(const rectangleType& rectangle) const
{
    rectangleType temp = *this;

    temp.length = rectangle.length;
    temp.width = rectangle.width;

    return temp;
}
rectangleType rectangleType::operator-(const rectangleType& rectangle) const
{
    rectangleType temp = *this;

    if(temp.length - rectangle.length >= 1 && temp.width - rectangle.width >= 1)
    {
        temp.length = temp.length - rectangle.length;
        temp.width = temp.width - rectangle.width;

        return temp;
    }
    else
    {
        cout << endl << "Dimensions are not large enough."
             << "Cancelling operation and returning dimensions"
             << "of left operand." << endl;
    }

    return temp;    
}

In the main file, I have created the following objects:

rectangleType myOtherYard(26, 19);
rectangleType myBrothersYard(2, 2);
rectangleType myMothersYard(3, 3);

and written this code:

myOtherYard = myBrothersYard;
cout << endl << "myOtherYard = myBrothersYard: " 
     << myOtherYard;
cout << endl << "myBrothersYard - myMothersYard: " 
     << myBrothersYard + myMothersYard;

Here's the output I get (using formatted printing):

myOtherYard = myBrothersYard:   26.0019.00 
myBrothersYard - myMothersYard: 3.003.00

It looks like there is no assignment going on in the = operator and it's returning the length and width of the first object without change. Also, the - operator seems to be doing its job but returning length and width separately and I don't know how to get it to return the area. Everything I've tried in code has failed.

The + operator adds and returns separate length and width values. It does look like it's adding them correctly.

Any way you can help me understand how to fix this?

Was it helpful?

Solution

First, your assignment operation should return a reference to yourself. It should not be const either, since you are assigning to yourself. See many references, mainly Scott Meyers "Effective C++", for reasoning.

Since you are just learning, and I don't see it in there, I assume you are not supposed to know about move semantics, so I'll leave that out...

Assignment operator from another rectangle...

rectangleType &
rectangleType::
operator=(rectangleType const & that)
{
    length = that.length;
    width = that.width;

    return *this;
}

However, it looks like the default assignment operator would be sufficient. If you are required to write the assignment operator for class, I imagine you also have to write the copy constructor as well.

If you are going to have an operator- you should also have operator-=. Again, consult Meyers for lots of explanation.

On your subtraction implementation... Egads. Really? cout for an error? and then return an object that is not the right answer?

OTHER TIPS

Something like that may help:

rectangleType& rectangleType::operator=(const rectangleType& rectangle) 
{
    if (this != &rectangle)
    {
         length = rectangle.length;
         width = rectangle.width;
    }

    return *this;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top