Overloading operator for complex numbers

I have a program written to add 2 complex numbers. Everything is working, except when the sum gets written, it gives me a number that is way off. Can you see what is going wrong?

THANK YOU.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <complex>
#include <fstream>
#include <cstdlib>
#include <math.h>
class complex {

public:
   complex();
   complex(double r, double i){
      r = 0;
      i = 0;
   }
   complex(const complex& c);

   complex operator+ (const complex& c){
      //complex result;
      //result.r = this->r + c.r;
      //result.i = this->i + c.i;
      //return result;
      return complex(c.r + this->r, c.i + this->i);
   }
friend std::ostream &operator<< (std::ostream &output, complex &c);
   friend std::istream &operator>> (std::istream &input, complex& c);
protected:
   double r;
   double i;

private:
};

std::istream& operator>> (std::istream &input, complex& c){
   char plus;
   char i;
   input >> c.r >> plus >> c.i >> i;
   return input;
   //return c.a >> c.b;
}
std::ostream &operator<< (std::ostream &output, complex &c){
   output << c.r << "+" << c.i << "i";
   return output;
   //return "a: " << c.a << "b: " << c.b;
}
int main(){

complex x(0, 0);
complex y(0, 0);

std::cout << "Enter a complex number (a+bi) : " << std::endl;
std::cin >> x;

std::cout << "Enter a complex number (a+bi) : " << std::endl;
std::cin >> y;
std::cout << "x " << x << std::endl;
std::cout << "y " << y << std::endl;
complex z = x + y;
std::cout << z << std::endl;
return 0; 
}


and my output ends up being
Enter a complex number (a+bi) : 
1+2i
Enter a complex number (a+bi) : 
2+3i
x 1+2i
y 2+3i
4.8784e-270+4.85593e-270i
Let satrt from the constructor

1
2
3
4
   complex(double r, double i){
      r = 0;
      i = 0;
   }


that has no any sense. it has two parameters that are assigned 0 in the body of the constructor.
Whoops. That must have come across in copy and paste.
in my code it is just
1
2
complex(double r, double i){
}
And what does this mean?!

1
2
complex(double r, double i){
}


There is no more sense than in the previsos realization of the constructor.
Last edited on
it's just a constructor that takes 2 arguments. I had to include it as part of my assignment. is that what is tripping me up? i'm not entirely confident on what it means.
If you have to include it as part of your assignment when wtite it correctly.
what is wrong with the way it is written?
it does nothing.
I suspect that you want it to do something.
Why have two parameters that you're going to do nothing with?
oh right! so how about this
1
2
3
4
complex(double r, double i){
   this->r = r;
   this->i = i;
}
Now try your program anew.
fixed that part! i was including <complex> which messed up the whole thing!

i also have to overload some member functions like conj

1
2
3
4
5
 complex conj(const complex& c){
      this->r = c.r;// = this->a;
      this->i = c.i * -1;// = this->b * -1;
      return complex (this->r, this->i);
   }


it is all in my own namepspace called complex_numbers

how would i call this in main?
Last edited on
For example

complex_numbers::complex x(0, 0);
Topic archived. No new replies allowed.