1. ## How do I Overload these operators in this program?

I have a program in which you have to overload the operators (all under a class called ComplexNum) >>, <<, +, -, /, and *. My program is that I must take two sets of numbers, each set having two numbers, and manipulate each with these operators. But here is the kicker, one of the numbers is an imaginary number. Ok, here is an example:
---------------
Input file:
2.5 -2.2
1.0 1.0
Output (what it is suppose to be)
A = (2.5) + (-2.2)i
B = (1.0) + (1.0)i

A + B = (3.5) + (-1.2)i ....and so on
---------------
Ok, I have been given for example, what to do with the overloaded assignment operator, such as
>> extracts two parts of the complex number (or)
<< inserts the complex number with (a) + (b)i format (or)
+ is (a + c) + i(b+d)
* is (ac-bd) + i(bc + ad)
....so on

But how do I implent or do any of this? I was not instructed very well, and any help with the coding would be apreciated. Thanks,
aaron

2. Code:
```class ComplexNum
{
double real, imaginary;
public:
// Constructor
ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}
// Copy constructor
ComplexNum( const ComplexNum& rhs )
{
real = rhs.real;
imaginary = rhs.imaginary;
}
friend ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs );
friend ostream& operator<<(ostream& out,const ComplexNum& rhs );
};

ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs )
{
return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
}

ostream& operator<<(ostream& out,const ComplexNum& rhs )
{
return out << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
}

int main()
{
ComplexNum A(2.5,-2.2), B(1.0,1.0), C;

C = A + B;

cout << "A is: " << A << endl;
cout << "B is: " << B << endl;
cout << "C is: " << C << endl;

return 0;
}```
Output:
Code:
```A is: (2.5) + (-2.2)i
B is: (1) + (1)i
C is: (3.5) + (-1.2)i```
You can do the other operators yourself I'm sure.

3. Hey, cool. Thanks a lot for your help!!!

4. Quick question, I am going to be reading from an input file. How do I read two numbers from the input file and put them both into A like this? I belive this is why I have to overload the >> function?

5. I would guess you are supposed to implement the operator>> in order to read the numbers from your input file. Try and have a go at it.

Code:
```class ComplexNum
{
...
public:
...
friend istream& operator>>(istream& is, ComplexNum& rhs );
};

istream& operator>>(istream& is, ComplexNum& rhs )
{
// Figure out what to put here
}```

6. cross posted.

7. Ok, I am now getting these errors with this code:
Code:
```#include <fstream.h>
#include <ostream.h>

class ComplexNum
{
public:
float real;
float imaginary;
ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}
ComplexNum(const ComplexNum& rhs)
{
real = rhs.real;
imaginary = rhs.imaginary;
}
friend ostream& operator<<(ostream& outFile, const ComplexNum&
rhs);
friend istream& operator>>(istream& inFile, ComplexNum& rhs);
friend ComplexNum operator+(ostream& outFile, const ComplexNum&
rhs);
};

ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs)
{
return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
}

ostream& operator<<(ostream& outFile, const ComplexNum& rhs)
{
return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
}```
Now, these errors:
/tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
: multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x0): first defined here
/tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
: multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x44): first defined here
collect2: ld returned 1 exit status

Any ideas why??

Ok, I am now getting these errors with this code:
Code:
```#include <fstream.h>  // Use <fstream> instead
#include <ostream.h>  // Use <iostream> instead
using namespace std;  // Add this

class ComplexNum
{
public:
...
friend ostream& operator<<(ostream& outFile, const ComplexNum& rhs);
friend istream& operator>>(istream& inFile, ComplexNum& rhs);
friend ComplexNum operator+(ostream& outFile, const ComplexNum&
rhs);
};

ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs)
{
return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
}

ostream& operator<<(ostream& outFile, const ComplexNum& rhs)
{
return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
}```
Now, these errors:
/tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
: multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x0): first defined here
/tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
: multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x44): first defined here
collect2: ld returned 1 exit status

Any ideas why??
Your definition of operator+ does not match the declaration of it. These need to be the same. The operator+ function should take two const ComplexNum objects as arguments. What is that ostream object doing there?

You really should be using the newer headers (the ones without the .h extension). I.e. fstream instead of fstream.h and so on. As far as the ostream header, you are going to need the istream header as well since you are also going to be dealing with input streams so you might as well just include the iostream header which includes everything you need for both input and output streams. Using the newer headers means you will have to deal with namespaces, at this stage it is probably easiest to just put a using namespace std; line after all your header statements.

Don't know specifically what might be causing the problem with the operator<< function. Make the other fixes and see if you still get the error.

9. Cool, no errors. Yeah, that was just a stupid mistake when I put in ostream into the function declaration instead of the other const ComplexNum. I changed everything around though, and I have no errors; I am gonna finish coding it, so I will keep you posted if it all goes well (which I don't see why it shouldn't).

10. All works fine. Thanks once again for your help.