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

# operator+ for complex numbers

 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. Jun 26 '06 #1
7 Replies

 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. Jun 26 '06 #2

 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. Jun 26 '06 #4

 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 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. 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 e-mail I do not respond to top-posted replies, please don't ask Jun 26 '06 #5

 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. Jun 26 '06 #6

 P: n/a "john134" 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 Jun 27 '06 #7

 P: n/a it worked, thanks a lot Bill Shortall wrote: "john134" 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 Jun 29 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.