I need Help With My Class Object

I don't Know Why The Result Is correct for the subtraction -=
and wrong for sum +=

Need Help


#include<iostream>
using namespace std;
class complex {
float r , im ;
public:
complex (float a ,float b)
{
r=a;
im=b;
}
complex& operator+= (complex &x)
{
r=r+x.r;
im=im+x.im;
return * this;
}
complex& operator-= (complex &x)
{
r=r-x.r;
im=im-x.im;
return * this;
}
complex& operator*= (complex &x)
{
r=r*x.r;
im=im*x.im;
return * this;
}
complex& operator/= (complex &x)
{
r=r/x.r;
im=im/x.im;
return * this;
}
friend void show (complex &x)
{cout<<x.r<<"+i"<<x.im<<"\n";}


};
void main()
{
complex m(10,20) , n(15,30);
m-=n;
show(m);
m+=n;
show(m);
system("pause");
}
isn't it correct for both of them?

The result should be 10 + 20i and that seems to be the case
yep I agree with Gamer.
No the sum must me 25+50 i
not 10+20 i

because 10+15=20
and 20+30 =50

So ,,, what is the problem
Yeah, but when doing m -= n you modify m, right?
So after m-=n you have -5 | -10
after that you do m+=n and you should get 10|20 then again
Yes I know i modified it

but i want to know how to make "m+=n" use the numbers without modification of "m-=n"


what i meant that i want to "m+=n" to use number (10,15) and (20,30) even after using
"m-=n"


I hope you get it because my English is bad :(
the += or -= operator are used to modify an object

You could reset m to 10|15
Or you could use the + and - operator instead....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class complex {

// stuff

  complex operator+(complex x)
  {
    x += *this;
    return x;
  }
  complex operator- (complex x)
  {
    x -= *this;
    return x;
  }
};

// ... 


void main()
{
    complex m(10,20) , n(15,30);
    complex z = m-n;
    show(z);
    z = m+n;
    show(z);
    system("pause");
} 
Topic archived. No new replies allowed.