0

I have a few questions about good practices, I have this piece of code in c++:

class Complex{
        friend double Re(Complex &z);
        friend double Im(Complex &z);
        friend Complex operator + (Complex &augend, Complex &adend);
        friend Complex operator - (Complex &minuend, Complex &substraend);
        friend Complex operator * (Complex &multiplier, Complex &multiplicand);
    public:
        Complex &operator = (Complex &z); 
        Complex(double real, double imaginary);
        void print(void);
    private:
        double real, imaginary;
};

With functions like:

double Re(Complex &z){
    return z.real;
}

With some operators overloaded like:

Complex operator + (Complex &z, Complex &w){
    return Complex(
        z.real + w.real,
        z.imaginary + w.imaginary);

}

If I can get the real and imaginary private variables using Re() and Im() functions that return them, should I use friend functions that use private variables, or should I write the operators in terms of these functions, something like

Complex operator + (Complex &z, Complex &w){
        return Complex(
            Re(z) + Re(w),
            Im(z) + Im(w));
    }

Also, is there a point in making real, and imaginary variables private if I'm gonna use them outside class anyway with those functions?

Another question, I don't see the propourse in overload = operator like this:

Complex& Complex::operator = (Complex &z){
    this->real = z.real;
    this->imaginary = z.imaginary;
    return *this;
}

If I don't do it, I can do something like Complex C = A + B with A and B complex clases, in the main anyway.

And last question, what is the best practice? To declare functions with variables like this?:

friend Complex operator + (Complex &augend, Complex &adend);

or should I declare without them like this?:

friend Complex operator + (Complex &, Complex &);

?

  • I use `friend` almost only for overloaded operator implementations otherwise you need a whole load of accessor functions which will serve to circumvent encapsulation. I also like to include variable names in function declarations to aid self-documentation. I believe these approaches are pretty standard. Use `const` references though if possible else you might / should / will get curious errors when binding temporaries. It's also a better thing to do. – Bathsheba Sep 15 '22 at 12:49
  • 1
    *"what is the best practice?..."* Seems opinion based. See [When should you use 'friend' in C++?](https://stackoverflow.com/questions/17434/when-should-you-use-friend-in-c) and [When do I have to use a friend function rather than a member function?](https://stackoverflow.com/questions/16756034/when-do-i-have-to-use-a-friend-function-rather-than-a-member-function) – Jason Sep 15 '22 at 12:50
  • Start with `+=`, `-=`, and `*=` as member functions, then implement `+`, `-`, and `*` in terms of those. No friendship needed. – molbdnilo Sep 15 '22 at 12:51
  • Also, one question per post. – Jason Sep 15 '22 at 12:52
  • See [here](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading), scroll down to "Binary arithmetic operators". – molbdnilo Sep 15 '22 at 12:59

0 Answers0