So, we are studying about the topics I mentioned in the title and it seems like I got it all pretty confused.
Let's say for example that I have this class:
class Complex {
double re;
double im;
public:
Complex(double r = 0, double i = 0);
}
Now, I want to be able to do some basic arithmetic's with complex numbers.
For example, let's say I want to add one complex number to another, for this I can add the following method:
class Complex {
double re;
double im;
public:
Complex(double r = 0, double i = 0);
Complex& operator+=(const Complex& c);
}
And it will be defined like this:
Complex& Complex::operator+=(const Complex& c) {
re += c.re;
im += c.im;
return *this;
}
However, when I want to compare two complex numbers, to my understanding things become trickier, and for some reason which I am unable to understand we will have to add "friend" in the before we add the declaration of the function.
The function is defined like this:
bool operator==(const Complex& a, const Complex& b) {
return a.re == b.re && a.im == b.im;
}
So my questions are:
Why don't we add "friend" before declaring the operator += as well? I mean both times we use the same variables in the class.
How is it possible to determine where friend should be used?