P: n/a

Complex Complex::operator+( 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.  
Share this Question
P: n/a

"john134" wrote: Complex Complex::operator+( 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.  
P: n/a

john134 wrote: Complex Complex::operator+( 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 nonmembers.
V

Please remove capital 'A's when replying by email
I do not respond to topposted replies, please don't ask  
P: n/a

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?
osmium wrote: "john134" wrote:
Complex Complex::operator+( 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.  
P: n/a

john134 wrote: 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 toppost.
No, there is no "easier" way. You will have to implement a nonmember.
So either implement this one as a nonmember, or implement another one
and use the one you have to actually provide the functionality.
osmium wrote: "john134" wrote:
Complex Complex::operator+( 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

Please remove capital 'A's when replying by email
I do not respond to topposted replies, please don't ask  
P: n/a

"john134" wrote: 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.  
P: n/a

"john134" <jo********@gmail.com> wrote in message
news:11*********************@b68g2000cwa.googlegro ups.com... Complex Complex::operator+( 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  
P: n/a

it worked, thanks a lot
Bill Shortall wrote: "john134" <jo********@gmail.com> wrote in message news:11*********************@b68g2000cwa.googlegro ups.com... Complex Complex::operator+( 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
  This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 4003
 replies: 7
 date asked: Jun 26 '06
