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?
HEADER FILE:
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;
/*post: overloaded input */
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;
}