# Thread: Ugh, this coding is killing me

Hello again. I need help with operation overloading with the >> input arrows. The previous threads did not help me much. I'm working with computing complex numbers. Can anybody tell me what I'm doing wrong in trying to redefine my input arrows?

Code:
```#include <iostream>
#include <cmath>

using namespace std;

namespace Evan
{
class Complex
{
private:
double real;
double imag;
public:
Complex();
/*post: default constructor */
Complex(double notReal, double notImag);
/*post: non-default constructor */
Complex operator+(const Complex& other) const;
friend istream& operator>>(Complex c1, Complex c2);
double getReal() const;
/*post: gets the real numbers*/
double getImag() const;
/*post: gets the imaginary numbers*/
void setReal(double notReal);
/*post: sets the real numbers*/
void setImag(double notImag);
/*post: sets the imaginary numbers */
double findMagnitude();
/*post: finds the magnitude of the complex number */
};
}

#endif```
IMPLEMENTATION FILE:
Code:
```#include "Complex.h"

namespace Evan
{
istream& operator>>(Complex& c1, Complex& c2)
{
double notReal, notImag;
cin >> notReal >> notImag;
c1.setReal(notReal);
c1.setImag(notImag);
c2.setReal(notReal);
c2.setImag(notImag);
return cin;
}

Complex Complex::operator+(const Complex& other) const
{
Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}

Complex::Complex()
{
real = 0;
imag = 0;
}

Complex::Complex(double notReal, double notImag)
{
real = notReal;
imag = notImag;
}

double Complex::getReal() const
{
return real;
}

double Complex::getImag() const
{
return imag;
}

void Complex::setReal(double notReal)
{
real = notReal;
}

void Complex::setImag(double notImag)
{
imag = notImag;
}

double Complex::findMagnitude()
{
return sqrt(real*real + imag*imag);
}
}```
MAIN APPLICATION FILE:
Code:
```#include "Complex.h"

using namespace Evan;

int main()
{
double notReal, notImag;
Complex c1, c2;
cout << "Welcome to my crappy program." << endl;
cout << "Enter the real and imaginary numbers separated by a space for x1: ";
cin >> c1;
c1.setReal(notReal);
c1.setImag(notImag);
cout << "Enter the real and imaginary numbers separated by a space for x2: ";
cin >> c2;
c2.setReal(notReal);
c2.setImag(notImag);
return 0;
}```

2. An example of the operator overload of the input istream operator.
Code:
```// declaration in class file
friend istream &operator>>( istream &, Tester & );

// member defintion in cpp file
// i and j are two integer variables of the class Tester
istream &operator>>( istream &input, Tester &t )
{
cout << "Enter two numbers: ";
input >> t.i >> t.j;
return input;
}```

3. Originally Posted by bigtamscot
An example of the operator overload of the input istream operator.
Code:
```// declaration in class file
friend istream &operator>>( istream &, Tester & );

// member defintion in cpp file
// i and j are two integer variables of the class Tester
istream &operator>>( istream &input, Tester &t )
{
cout << "Enter two numbers: ";
input >> t.i >> t.j;
return input;
}```
I need to know how to do this for 2 different sets of numbers, though. Will this work for both sets?

4. Yes if you set it up right.

Code:
```// in complex.h
friend istream& operator>>(istream &input, Complex &c);

istream& operator>>(istream &input, Complex &c)
{
// since this method is a friend of Complex
// it can set the private members directly
// using the dot access
input >> c.real >> c.imag;

return input;
}

// now all that is needed is to call it in main for your two instances of Complex.

// get first set of complex numbers
cin >> c1;

// get second set
cin >> c2;```

5. Originally Posted by bigtamscot
Yes if you set it up right.
Works great now. Thank you very much for the help.