# arithmetic operator friend functions

• 02-06-2006
linucksrox
arithmetic operator friend functions
i can't quite figure out how the arithmetic operators are supposed to be used, as 2 parameters are being passed in:
Code:

```#include <iostream> #include <cmath> using namespace std; class complex { private:     double real; // real part     double imag; // imaginary part public:     complex(double x=0.0, double y=0.0);        double magnitude() const; // return magnitude     double realPart() const; // return real part     double imagPart() const; // return imaginary part         complex operator- ();     friend complex operator+ (const complex& lhs, const complex &rhs);     friend complex operator- (const complex& lhs, const complex &rhs);     friend complex operator* (const complex& lhs, const complex &rhs);     friend complex operator/ (const complex& lhs, const complex &rhs);     friend bool operator== (const complex& lhs, const complex &rhs);     friend ostream& operator << (ostream& ostr, const complex &x);     friend istream& operator >> (istream& istr,  complex &x); }; double complex::magnitude() const {     double temp = 0.0;     temp = (real * real) + (imag * imag);     temp = sqrt(temp); } double complex::realPart() const {     return real; } double complex::imagPart() const {     return imag; } complex operator+ (const complex& lhs, const complex &rhs) {     return complex(lhs.real - rhs.real, lhs.imag - rhs.imag); } complex operator- (const complex& lhs, const complex &rhs) {     return complex(lhs.real - rhs.real, lhs.imag - rhs.imag); } int main() {   // complex number i= 0 + 1i   complex i(0,1),  z1, z2;   // input values   cout << "Enter two complex numbers: ";   cin >> z1 >> z2;   cout << "Test the binary arithmetic operators:" << endl;   cout << " z1 + z2 = " << (z1 + z2) << endl;   cout << " z1 - z2 = " << (z1 - z2) << endl;   cout << " z1 * z2 = " << (z1 * z2) << endl;   cout << " z1 / z2 = " << (z1 / z2) << endl;   // test relational equality   if (z1 == z2)       cout << z1 << " = " << z2 << endl;   else       cout << z1 << " != " << z2 << endl;   // verify that i*i = -1 and that -i*i = 1   cout << "i*i = " << i*i << endl;     cout << "-i*i = " << -i*i << endl;   return 0; }```
• 02-06-2006
jwenting
Of course there are 2 parameters, each reflects one of the operands.

Code:

`x = y + z`
gets translated into
Code:

`x = operator+(&y, &z);`
• 02-06-2006
linucksrox
ok, that makes sense, but then why are both parameters a class of complex? that tells me that when i enter the addition function, now I am dealing with 3 class instances. what do i do with them? if what you say is true, then i would guess that either both parameters should be double or there should be only one parameter of the complex class. and my code gives me a few linker errors, saying something about complex::complex(double, double)
• 02-06-2006
Daved
Where are you getting the error? Why would doubles have anything to do with it? When using ints, you would have int a, b and c if a = b + c. Same with complex. Just because you can use doubles to initialize a complex instance, doesn't mean you need them around when adding them (except internally in the operator+ function).

When adding two objects, you don't modify the two objects, you just add them and create a third value that is their sum. If you want to modify one of the objects, you would use += instead of +.

Note that your operator+ actually does subtraction. Also not that there is an std::complex, so you should be careful about calling your own class that, especially when you are using a usingdirective for the std namespace.

Finally, a common way to implement operator+ and the other three arithmetic operators is to implement += first (along with -=, *= and /=), and then use += to do the work of operator+:
Code:

```complex operator+ (const complex& lhs, const complex &rhs) {         complex temp(lhs);         temp += rhs;         return temp; }```
>> x = operator+(&y, &z);
Technically it is just translated to x = operator+(y, z);
• 02-06-2006
linucksrox
i get 4 errors saying
[Linker error] undefined reference to `complex::complex(double, double)'
and one saying [Linker error] undefined reference to `complex::operator-()'
• 02-06-2006
Daved
You have several functions in complex that you declared and use but you never implement. In the posted code there is no implementation of the constructor that takes two doubles. There is no implementation of operator*, operator/, operator==, operator<< or operator>>. You've got to implement those before it will link (or comment out the code in main that uses them).
• 02-06-2006
linucksrox
wow... i forgot to implement the default constructor. that might help just a little. and the rest of the functions, i just didn't implement them in the code i posted. i think i got it under control now though. thanks
• 02-06-2006
jwenting
while in the operator you indeed momentarilly have 3 instances, but that's to be expected.
There are the two operands and then there's the result.

Gets more interesting when you get around to implementing operators to allow adding of real numbers to complex numbers.