Overloading !

Hello
I need help with overloading '==' and '*'
i cant seem to find the right way to do it, can somebody help please
here is my code:
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
86
87
88
89
90
 #include <iostream>
using namespace std;

class Complax
{
private:
	double real;
	double imag;

public:
	Complax()
	{
		real = 0;
		imag = 0;
	}

	Complax(double x, double y)
	{
		real = x;
		imag = y;

	}

	void setReal(double x)
	{
		real = x;
	}

	void setImag(double x)
	{
		imag = x;
	}

	double getReal()
	{
		return real;
	}

	double getImag()
	{
		return imag;
	}

	Complax operator =(Complax &x)
	{
		real = x.getReal();
		imag = x.getImag();
	}

	Complax operator +(Complax &x)
	{
		Complax temp;
		temp.setReal(x.getReal() + real);
		temp.setImag(x.getImag() + imag);
		return temp;

	}

	Complax operator -(Complax &x)
	{
		Complax temp;
		temp.setReal(x.getReal() + real);
		temp.setImag(x.getImag() + imag);
		return temp;

	}

};



int main()
{
	Complax x(1, 2);
	Complax y(2, 3);
	Complax z, w, v;

	z = x + y;
	w = x - y;
	v = x * y;

	if (z == w)
		cout << " z = w" << endl;
	else
		cout << " z != w" << endl;


	system("PAUSE");
	return 0;
}
Last edited on
You weren't specific on what these operators should do. IIRC, Complex numbers multiply/compare equal by doing said operation with their real component and imaginary component. For operator==, make a global function like this:

1
2
3
4
bool operator==(const Complax& a, const Complax& b) {
   return( a.getReal() == b.getReal() &&
              a.getImag() == b.getImag() );
}


For operator*, see if you can figure it on your own ;) but hint: It's going to look a lot like and follow the same principle as your operators - and +.

Btw, your operator-() is doing a + of your operands. Lastly your operators *, -, +, and = should accept a const reference, that way you can pass const objects and temporary objects, and it prevents you from accidently modifying your param.
I added some stuff to the code but it's showing me errors in the code and there's no obvious one

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;

class Complax
{
private:
	double real;
	double imag;

public:
	Complax()
	{
		real = 0;
		imag = 0;
	}

	Complax(double x, double y)
	{
		real = x;
		imag = y;

	}

	void setReal(double x)
	{
		real = x;
	}

	void setImag(double x)
	{
		imag = x;
	}

	double getReal()
	{
		return real;
	}

	double getImag()
	{
		return imag;
	}

	Complax operator =(Complax &x)
	{
		real = x.getReal();
		imag = x.getImag();
	}

	Complax operator +(Complax &x)
	{
		Complax temp;
		temp.setReal(x.getReal() + real);
		temp.setImag(x.getImag() + imag);
		return temp;

	}

	Complax operator -(Complax &x)
	{
		Complax temp;
		temp.setReal(x.getReal() + real);
		temp.setImag(x.getImag() + imag);
		return temp;

	}
	Complax operator *(Complax &x)
	{
		Complax temp;
		temp.setReal(x.getReal() * real);
		temp.setImag(x.getImag() * imag);
		return temp;

	}

	bool operator ==(const Complax& a, const Complax& b) {
	
		return (a.getReal() == b.getReal() && a.getImag() == b.getImag());
	}

};



int main()
{
	Complax x(1, 2);
	Complax y(2, 3);
	Complax z, w, v;

	z = x + y;
	w = x - y;
	v = x * y;

	if (z == w)
		cout << " z = w" << endl;
	else
		cout << " z != w" << endl;

	cout << "z= " << z << endl << "w= " << w << endl << "v= " << v;
	system("PAUSE");
	return 0;
}
The operator== needs to be a *global* function, ie, outside the body of the class, such as where line 82 is currently. You'll also need a global operator<<(ostream&, const Complax&) function in order to send out z, w, and v on line 100. It could look something like this:

1
2
3
4
5
6
//Return an ostream& to allow chaining of << operators.
ostream& operator<<(ostream& out, const Complax& c)
{
   out << c.getReal() << '+' << c.getImag() << 'i';  //Just my guess on how it should look, feel free to change this if you want it to look different.
   return( out );  //This allows the chaining.
}


And don't forget what I said previously about your operator-(), and the const refs! ;)
Thank you so much,

it helped me a lot, but i have a question about '=' overload, it says it has to return value, what value it should return ? or my format is wrong ?

1
2
3
4
5
6
Complax operator =(Complax &x)
	{
		real = x.getReal();
		imag = x.getImag();
	}
1
2
3
4
5
6
    Complax& operator =(const Complax &x)
    {
        real = x.getReal();
        imag = x.getImag();
        return *this;
    }


Of course, getReal and getImage should be const (as well as operator+ and operator-.)
Worked!
Thanks a lot guys
Topic archived. No new replies allowed.