Operators

So i have to write an !operator that changes the symbol ( if its + it shows -, and if its - it shows +) of a complex number of my choice, and i dont really understand how to write it, can you help me rewrite the !operator ?

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using std::cout;
using std::endl;
using namespace std;

class Complex
{
 public:
 Complex(double=0, double=0);
 void setReal(double);
 double getReal();
 void setImaginar(double);
 double getImaginar();
 Complex operator+(Complex);
 Complex operator*(Complex);
 
 Complex operator!(){
Complex nc;
 nc.setReal= -nc.setReal;
 nc.setImaginar= -nc.setImaginar;
 return nc;
 }

 private:
 double real;
 double imaginar;
};

Complex:: Complex(double r, double i)
{
 real = r;
 imaginar = i;
}
void Complex::setReal(double r)
{
 real = r;
}
double Complex::getReal()
{
 return real;
}
void Complex::setImaginar(double i)
{
 imaginar = i;
}
double Complex::getImaginar()
{
 return imaginar;
}

Complex Complex::operator+(Complex c)

{
 Complex nc;
 nc.setReal(real+c.real);
 nc.setImaginar(imaginar+c.imaginar);
 return nc;
}

Complex Complex::operator*(Complex c)

{
 Complex nc;
 nc.setReal(real*c.real);
 nc.setImaginar(imaginar*c.imaginar);
 return nc;
}


int main()
{
 Complex n1(2,4);
 Complex n2(1,-4);
 Complex n3(0.0);
 Complex n4(0.0);
 Complex n5(0,0);
 n3 = n1+n2;
 cout<<"Adunarea numerelor complexe: "<<"["<< n3.getReal() << ";" << n3.getImaginar() << "]"<<  endl;
 n4=n1*n2;
 cout<<"Inmultirea numerelor complexe: "<<"["<< n4.getReal() << ";" << n4.getImaginar() << "]"<<  endl;
 n5=!n1;
 cout<< n5.getReal()<<";"<<n5.getImaginar()<<endl;
 return 0;
}
Last edited on
Are you sure that you didn't mean complex conjugate?


BTW, not related, but your complex number multiplication is wrong.
yea sorry, complex conjugate, and yes now i see i multipicated wrong.
Complex Complex::operator*(Complex c)

1
2
3
4
5
6
{
 Complex nc;
 nc.setReal(real*c.real-imaginar*c.imaginar );
 nc.setImaginar(real*c.imaginar+imaginar*c.real);
 return nc;
}


i've change the multiplication
Yes, your complex multiplication now looks correct.

But if you can do that, then you should be able to do the complex conjugation in the same way. The real and imaginary parts would just be
real
-imaginar
and they could be set exactly as you have done above.
It just says Complex operator!(Complex); need void
Why do you need void? The complex conjugate is a complex number; that's what you should return.

1
2
3
4
5
6
7
Complex Complex::operator !()
{
   Complex nc;
   // set your real part
   // set your new imaginary part
   return nc;
}
Last edited on
Topic archived. No new replies allowed.