0

I am overloading operator '/'. when I divide object 'a' with object 'b' (under main), object 'a' is changed to 2, instead of equaling 7, as it was previously to the division. Is this an error? If not, why is the 'a'=2 and not 7?

#include <iostream> 
using namespace std;
class OO{
  int x;
  int y;
  public:
         OO(){x=y=0;}
         OO(int i, int j){x=i;y=j;}
         OO operator+(OO ob){
                         OO temp;
                         temp.x=x+ob.x;
                         return temp;
                         }
         OO operator=(OO ob){
                        x=ob.x;
                        return *this;
                        } 
         OO operator/(OO ob){
                        x=x/ob.x;
                        return *this;
                        }
         OO operator++(){
                         OO temp;
                         temp.x=++x;
                         return temp;

                         }                                            
         void show(){

              cout<<x<<endl;

              }                             

  };


int main() {
OO a(6,2);
OO b(3,2);
b.show();
a.show();
OO c = a + a;
c.show();
++a;
a.show();
c = a / b;
a.show();//why is this 'a' = 2, instead of 7?
c.show();
c=b=a;
c.show();
}
  • 1
    The expression `a/b` does not typically change `a`. Do you see that your implementation of `operator/` **does** change the left operand? – Drew Dormann May 22 '19 at 00:02
  • 1
    Related: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/). – Remy Lebeau May 22 '19 at 00:50

2 Answers2

2

You're modifying a inside the operator on this line:

x=x/ob.x;

That modifies x. What you want is what you did for operator+(). That is, create a temp object and return that instead:

OO operator/(OO ob){
    OO temp;
    temp.x=x/ob.x;
    return temp;
}
  • 1
    Similarly, the `operator++` is also implemented backwards. `OO` is implementing the **PRE**-increment operator, which is meant to increment the object it is called on and then return a *reference* to the modified object, so DON'T return a temp object in that operator: `OO& operator++(){ ++x; return *this; }` The **POST**-increment operator, on the other hand, is meant to modify the object and return a *copy* of the object before it was modified, so DO return a temp object in that operator: `OO operator++(int){ OO temp; temp.x=x++; return temp; }` – Remy Lebeau May 22 '19 at 00:23
  • Good point. I thought there might be something funky about `operator++()` but wasn't sure exactly what it was. –  May 22 '19 at 00:46
  • Keep in mind, that error won't create **this** problem though. It is just bad design that will cause errors in other code ;) –  May 22 '19 at 00:48
0

When you call c = a / b, your compiler understands a.operator/(b). That means your implementation will actually change the value of a and the result will be the division of a / b, which in this case is 7/3. However 7/3 is a division between integers, thus the result will be truncated and that will net you the number 2.

John M.
  • 245
  • 1
  • 3
  • 13