448,531 Members | 837 Online
Need help? Post your question and get tips & solutions from a community of 448,531 IT Pros & Developers. It's quick & easy.

# += operation

 P: n/a ComplexNum ComplexNum::operator +(const ComplexNum& rhs) const // - operation is the similar way { ComplexNum ans; ans.real = real + rhs.real; ans.imaginary = imaginary + rhs.imaginary; return ans; } it works and my lecturer said it is correct but, for += operation, my code is: ComplexNum ComplexNum::operator += (const ComplexNum& rhs) const { ComplexNum ans; ans.real += rhs.real; ans.imaginary += rhs.imaginary; return ans; } here is lecturer's comment: += should be different. a += b means a = a + b therefore *this object (i.e. a) should be updated with the answer. Do anyone know how to write += operation? Jun 27 '08 #1
16 Replies

 P: n/a fo***********@gmail.com writes: for += operation, my code is: ComplexNum ComplexNum::operator += (const ComplexNum& rhs) const { ComplexNum ans; ans.real += rhs.real; ans.imaginary += rhs.imaginary; return ans; } here is lecturer's comment: += should be different. a += b means a = a + b Your lecturer should've also told you that your results are undefined since the contents of the ans object are, apparently, not initialized. Your += operation on ans's members are applied to, apparently, uninitialized members of this object. This would be the same as writing: int x; x += 4; Obviously, the results of this are undefined. therefore *this object (i.e. a) should be updated with the answer. Correct. Do anyone know how to write += operation? Yes, but why don't you try to figure this out yourself. Look around, there are plenty of examples of assignment operators for other classes. Here are two free clues: 1) operator += modifies this object, therefore it cannot be a const function (well, it can be, but you're not there, yet). 2) The assignment operator, be it operator =(), operator +=(), operator -=(), or anything else, traditional does not return another instance of the object, but rather a reference to this, modified, object. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEABECAAYFAkhT81gACgkQx9p3GYHlUOKqFgCfTH21mCsaDH j6G3oDZ8WKtuw9 VMMAn3BxWQphDs5vEcoBgvba0JX5/fcu =KvB4 -----END PGP SIGNATURE----- Jun 27 '08 #2

 P: n/a On Jun 15, 12:35 am, Sam

 P: n/a On Jun 15, 12:35 am, Sam

 P: n/a Hi! Sam schrieb: This would be the same as writing: int x; x += 4; The class could have a default constructor which initializes the members, though. Frank Jun 27 '08 #5

 P: n/a Hi! fo***********@gmail.com schrieb: Of course I have searched the Internet but, on Google, I type += operation, it cannot detect '+='. That's why I find unsuitable ones. This should help: http://www.google.com/search?q=c%2B%...btnG=Suche&lr= Regards, Frank Jun 27 '08 #6

 P: n/a On Jun 14, 10:08*pm, "foolsmart2...@gmail.com"

 P: n/a fo***********@gmail.com writes: > is this time correct? ComplexNum operator += (const ComplexNum& in1, const ComplexNum& in2) { ComplexNum ans; ans.real = in1.real + in2.real; ans.imaginary = in1.imaginary + in2.imaginary; return ans; } I make it a friend function. This is only correct in the sense that this is syntactically valid C++ code, but this is not the correct implementation of the += operator. What you have above is really operator +, the regular addition operator. There's no difference between operator +, and your operator +=. The crucial difference is that the += operator modifies its left operand, and operator + does not. operator + returns a new object, that's defined by the addition operator on its two operands. This is really what your code, above, does, and it's not the same thing as the += operator. The += operator modifies its left-hand operator, while your code leaves it alone. With your implementation of the += operator, the following statement: in1 += in2; leaves the value of in1 completely unmodified, while in practice you expect in1's value to be changed by the += operator. Run a test program, using your friend function implementation of the += operator, and see for yourself. This is clearly wrong. Also, the assignment operators are NOT usually implemented as friend functions, but as regular class methods. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEABECAAYFAkhUC6QACgkQx9p3GYHlUOKQjgCfaf+CDFIW13 l/BCkUxFZzWIvK /c8AniaTgb12ug+mLbSYg2WRywYuiLG7 =eZoj -----END PGP SIGNATURE----- Jun 27 '08 #8

 P: n/a On 2008-06-14 18:20, fo***********@gmail.com wrote: ComplexNum ComplexNum::operator +(const ComplexNum& rhs) const // - operation is the similar way { ComplexNum ans; ans.real = real + rhs.real; ans.imaginary = imaginary + rhs.imaginary; return ans; } it works and my lecturer said it is correct but, for += operation, my code is: ComplexNum ComplexNum::operator += (const ComplexNum& rhs) const { ComplexNum ans; ans.real += rhs.real; ans.imaginary += rhs.imaginary; return ans; } here is lecturer's comment: += should be different. a += b means a = a + b therefore *this object (i.e. a) should be updated with the answer. Do anyone know how to write += operation? In the += case you want to add the values in rhs to the values of the complex number on the left hand side (which will be "this" in the body of the += operator. The += operator should also return a reference to the number on the left hand side instead of a new object containing the value of the operation. -- Erik WikstrĂ¶m Jun 27 '08 #9

 P: n/a "fo***********@gmail.com"

 P: n/a On Jun 15, 9:57 am, "Daniel T."

 P: n/a This time my code is: > ComplexNum& ComplexNum::operator += (const ComplexNum& rhs) { if(&rhs != this) { real+= rhs.real; imaginary += rhs.imaginary; } return *this; } is it correct? Why shouldn`t you allow addition to itself? I think it does make sense to allow it, this is another case as with the assignment operator. Jun 27 '08 #12

 P: n/a On Jun 15, 11:05 am, Sze

 P: n/a fo***********@gmail.com wrote: On Jun 15, 11:05 am, Sze

 P: n/a In article , Kai-Uwe Bux wrote: fo***********@gmail.com wrote: On Jun 15, 11:05 am, Sze

 P: n/a On Jun 15, 11:45 am, Kai-Uwe Bux

 P: n/a fo***********@gmail.com wrote: On Jun 15, 11:45 am, Kai-Uwe Bux foolsmart2...@gmail.com wrote: On Jun 15, 11:05 am, Sze Why shouldn`t you allow addition to itself?I think it does make sense to allow it, this is another case as withthe assignment operator. Do you mean this: ComplexNum& ComplexNum::operator += (const ComplexNum& rhs) { if(&rhs != this) { real= real+rhs.real; imaginary = imaginary+ rhs.imaginary; } return *this; } No.You made the first test case work. Now, also do this one (withoutbreaking the first):int main() { ComplexNum a( 1, 2 ); a += a; assert( a.real() == 2 ); assert( a.imag() == 4 ); cout << "OK\n";}BestKai-Uwe Bux for a +=a; this one, I have no idea. How can I do this? Hint: ask yourself why you insist on the if(&rhs != this) check. Best Kai-Uwe Bux Jun 27 '08 #17

### This discussion thread is closed

Replies have been disabled for this discussion.