operator+ for complex numbers

J

john134

Complex Complex::eek:perator+( const Complex &operand2 ) const {
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}

this is my addition operator for 2 complex numbers. e.g. C1(3.2, 1.5)
C2(2.1, 4.6) C1+C2 = (5.3, 6.1) How can i overload the operator+ to
make an addition between a double and a complex number? for example 3.0
+ C1 = (6.2, 1.5) Thanks.
 
O

osmium

john134 said:
Complex Complex::eek:perator+( const Complex &operand2 ) const {
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}

this is my addition operator for 2 complex numbers. e.g. C1(3.2, 1.5)
C2(2.1, 4.6) C1+C2 = (5.3, 6.1) How can i overload the operator+ to
make an addition between a double and a complex number? for example 3.0
+ C1 = (6.2, 1.5) Thanks.

Provide a conversion or cast from double to complex and make operator+ a
friend. You might want to try it with stub functions to see how well it
works at a "system" level before you do the actual coding. Not that the
actual coding is a big deal.
 
V

Victor Bazarov

john134 said:
Complex Complex::eek:perator+( const Complex &operand2 ) const {
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}

this is my addition operator for 2 complex numbers. e.g. C1(3.2, 1.5)
C2(2.1, 4.6) C1+C2 = (5.3, 6.1) How can i overload the operator+ to
make an addition between a double and a complex number? for example
3.0 + C1 = (6.2, 1.5) Thanks.

Is this a homework problem? Please read FAQ 5.2 then.

If this is not homework, please read Item 24 from "Effective C++", 3rd
edition (Item 19 in the 2nd Edition). For conversions to apply you
need those functions to be non-members.

V
 
J

john134

thanks for your reply but i should've mentioned that i'm a newbie
programmer. So the things that you've said like stub functions, system
level... don't make any sense to me. isn't there an easier way?
 
V

Victor Bazarov

john134 said:
thanks for your reply but i should've mentioned that i'm a newbie
programmer. So the things that you've said like stub functions, system
level... don't make any sense to me. isn't there an easier way?

Please don't top-post.

No, there is no "easier" way. You will have to implement a non-member.
So either implement this one as a non-member, or implement another one
and use the one you have to actually provide the functionality.

V
 
O

osmium

john134 said:
thanks for your reply but i should've mentioned that i'm a newbie
programmer. So the things that you've said like stub functions, system
level... don't make any sense to me. isn't there an easier way?

That *was* the easy way:)

A stub function is a function that, instead of doing what it was told,
prints a message simply saying that it was called.

Ex:
cout << "cast called\n";

The system level I mentioned was to give it a dry run. What do you have to
do to *use* the function? Is the syntax pleasing? Are all the interesting
cases taken care of? (At least as far as you know right now.)

There is no real *need* to do either of these, but I was trying to warn you
that I hadn't actually tried what I suggested. If the jargon bothers you,
proceed as was your tendency.
 
B

Bill Shortall

john134 said:
Complex Complex::eek:perator+( const Complex &operand2 ) const {
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}

this is my addition operator for 2 complex numbers. e.g. C1(3.2, 1.5)
C2(2.1, 4.6) C1+C2 = (5.3, 6.1) How can i overload the operator+ to
make an addition between a double and a complex number? for example 3.0
+ C1 = (6.2, 1.5) Thanks.

try this

Complex operator+( const Complex &op1, const double& op2 )
{
return Complex( op1.real + op2, op1. imaginary );
}

you might also want to add

Complex operator+( const double &op1, const Complex& op2 )
{
return Complex( op1+ op2.real , op2. imaginary );
}

note that these ae not class members so just add them in after the class
implementation
 
J

john134

it worked, thanks a lot

Bill said:
try this

Complex operator+( const Complex &op1, const double& op2 )
{
return Complex( op1.real + op2, op1. imaginary );
}

you might also want to add

Complex operator+( const double &op1, const Complex& op2 )
{

note that these ae not class members so just add them in after the class
implementation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top