473,503 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

+= operation

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 1508
Sam
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
On Jun 15, 12:35 am, Sam <s...@email-scan.comwrote:
foolsmart2...@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.

application_pgp-signature_part
1KDownload
Of course I have searched the Internet but, on Google, I type +=
operation, it cannot detect '+='. That's why I find unsuitable ones.
Jun 27 '08 #3
On Jun 15, 12:35 am, Sam <s...@email-scan.comwrote:
foolsmart2...@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.

application_pgp-signature_part
1KDownload
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.
friend ComplexNum operator+=(const ComplexNum& in1, const ComplexNum&
in2);
Jun 27 '08 #4
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
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
On Jun 14, 10:08*pm, "foolsmart2...@gmail.com"
<foolsmart2...@gmail.comwrote:
On Jun 15, 12:35 am, Sam <s...@email-scan.comwrote:
foolsmart2...@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.
*application_pgp-signature_part
1KDownload

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.
friend ComplexNum operator+=(const ComplexNum& in1, const ComplexNum&
in2);

No. It is not correct yet. The idea is to have operator+= as a non-
static member function, that's how you get a 'this' argument with it
and hence it would just be needing one argument instead of two.
Whatever you are doing with the local object 'ans' should be done with
the '*this' object.

When you get the operator+= working, as a next step, try to write
operator+ in terms of it (operator+=). So that, at least you have to
maintain the code of just one of those two operators.
Jun 27 '08 #7
Sam
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
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
"fo***********@gmail.com" <fo***********@gmail.comwrote:
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?
class ComplexNum
{
public:
ComplexNum& operator+=( const ComplexNum& rhs )
{
// add code here
return *this;
}
// everything else
};

int main() {
ComplexNum a( 1, 2 );
ComplexNum b( 3, 5 );
a += b;
assert( a.real() == 4 );
assert( a.imag() == 7 );
cout << "OK\n";
}

Your mission is to add code where it say "add code here" so that the
main function will print "OK" instead of asserting.
Jun 27 '08 #10
On Jun 15, 9:57 am, "Daniel T." <danie...@earthlink.netwrote:
"foolsmart2...@gmail.com" <foolsmart2...@gmail.comwrote:
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?

class ComplexNum
{
public:
ComplexNum& operator+=( const ComplexNum& rhs )
{
// add code here
return *this;
}
// everything else

};

int main() {
ComplexNum a( 1, 2 );
ComplexNum b( 3, 5 );
a += b;
assert( a.real() == 4 );
assert( a.imag() == 7 );
cout << "OK\n";

}

Your mission is to add code where it say "add code here" so that the
main function will print "OK" instead of asserting.
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?
Jun 27 '08 #11
Sze
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
On Jun 15, 11:05 am, Sze <kamistrik...@googlemail.comwrote:
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.
Do you mean this:
ComplexNum& ComplexNum::operator += (const ComplexNum& rhs)
{
if(&rhs != this)
{
real= real+rhs.real;
imaginary = imaginary+ rhs.imaginary;
}
return *this;
}

?
Jun 27 '08 #13
fo***********@gmail.com wrote:
On Jun 15, 11:05 am, Sze <kamistrik...@googlemail.comwrote:
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.

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 (without breaking
the first):

int main() {
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
cout << "OK\n";
}

Best

Kai-Uwe Bux
Jun 27 '08 #14
In article <g3**********@aioe.org>, Kai-Uwe Bux <jk********@gmx.net>
wrote:
fo***********@gmail.com wrote:
On Jun 15, 11:05 am, Sze <kamistrik...@googlemail.comwrote:
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.
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 (without breaking
the first):

int main() {
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
cout << "OK\n";
}
Here's a way to test both cases at once...

int main() {
{
ComplexNum a( 1, 2 );
ComplexNum b( 3, 5 );
a += b;
assert( a.real() == 4 );
assert( a.imag() == 7 );
}
{
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
}
cout << "OK\n";
}

Use the above main and try to get the program to print "OK".

(P.S., welcome to test-first programming. :-)
Jun 27 '08 #15
On Jun 15, 11:45 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
foolsmart2...@gmail.com wrote:
On Jun 15, 11:05 am, Sze <kamistrik...@googlemail.comwrote:
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.
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 (without breaking
the first):

int main() {
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
cout << "OK\n";

}

Best

Kai-Uwe Bux
for a +=a; this one, I have no idea. How can I do this?
Jun 27 '08 #16
fo***********@gmail.com wrote:
On Jun 15, 11:45 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>foolsmart2...@gmail.com wrote:
On Jun 15, 11:05 am, Sze <kamistrik...@googlemail.comwrote:
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.
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 (without
breaking the first):

int main() {
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
cout << "OK\n";

}

Best

Kai-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5879
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
4
1954
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to...
0
4107
by: John Jenkins | last post by:
Hi, any help on this would be greatly apprciated. I have been given a wsdl file from a customer generated by Oracle 10g Web Services tools. When I use wsdl.exe to attempt to produce proxy classes I...
1
11960
by: Laurent Lequenne | last post by:
Hello There, I just converted a VS 2003 C++ Project into VS 2005. I already made some changes in my headers files, has I had compilations errors with enums declarations. Now everything compiles...
0
1288
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
8
4829
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
6
2842
by: Cerebrus99 | last post by:
Hi all, I'm making a Windows application that does some lengthy retrieval operations from a database and possibly from a internet resource. I want to show that the operation is going on, by...
3
16539
by: JuHui | last post by:
I wrote a script to get 100 pages from a server. like below: 1:import httplib 2:conns = httplib.HTTPConnection("www.mytest.com") 3:conn.request("GET", "/") sometimes a socket error was...
2
4790
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
0
3116
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.