3

I have written a class for complex numbers in which I have overloaded the operator + and everything works fine, however I need to implement this as a non-member function and I am not sure how, or why there is a benefit of doing so.

Here is my code .h:

class Complex { private: double a; double b; public: Complex(); Complex(double aGiven); Complex(double aGiven, double bGiven); double aGetValue(); double bGetValue(); double operator[](bool getB); Complex add(Complex &secondRational); Complex operator+(Complex &secondRational); } 

.cpp:

Complex Complex::add(Complex &secondRational) { double c = secondRational.aGetValue(); double d = secondRational.bGetValue(); double anew = a+c; double bnew = b+d; return Complex(anew,bnew); } Complex Complex::operator+(Complex &secondRational) { return add(secondRational); } 

Any help on how to make these as non-member functions will be greatly appreciated!

2
  • possible duplicate of Operator overloading Commented Oct 9, 2013 at 21:06
  • 1
    You do need to read about const. Very urgently. Commented Oct 10, 2013 at 17:44

4 Answers 4

2

Here is the addition operator outside of the class:

Complex operator+(const Complex& lhs, const Complex& rhs) { //implement the math to add the two return Complex(lhs.aGetValue() + rhs.aGetValue(), lhs.bGetValue() + rhs.bGetValue()); } 

Of course you will need to declare aGetValue() and bGetValue() as const:

double aGetValue() const {return a;} double bGetValue() const {return b;} 
Sign up to request clarification or add additional context in comments.

Comments

1

The usual approach to arithmetic operations is to define the reflexive versions of the operators as members and the pure versions as non-members, implementing them with the reflexive versions:

class complex { public: const complex& operator+=(const complex& rhs) { real += rhs.real; imag += rhs.imag; return *this; } }; complex operator+(const complex& lhs, const complex& rhs) { complex res(lhs); res += rhs; return res; } 

18 Comments

Now you managed to confuse me. Why do you think it would be confusing? Considering int i = 0; (i+=1)+=2; is legal for integral types, why shouldn't it be legal for UDTs?
@TemplateRex Temporaries are no big deal if the objects are small, Complex here is just two doubles. But I used to work with Complex<T> and T was a base-10 high-precision custom type. Or think of matrices. This is where it becomes important to avoid temporaries and why libraries like my df.operators try to avoid them. And this is also why you should IMHO allow users to write "stupid" stuff like (i+=j)+=k;. Yes, a sane person would use i+=j; i+=k;, but my experience says I can't assume sane behaviour from others at all time :-)
@PeteBecker no the dupe was a side remark. I downvoted for the const ref because it's different from builtin behavior and standard libray practice, and quite possibly a performance pessimization.
T x; T& y = (x += 1); should be valid. Not because I want to use it, but because you do not have a good reason to prevent me from doing so. End of story!
"Do as the ints do" is a very basic rule of operator overloading and I happen to agree with that. This class' operator+=() violates that rule.
|
1

How is explained above by pippin1289.

Why is explained below:

Imagine one need to use object of class as

Complex c3 = 5 + c1;// for c3 object c1's real part (a) added with 5 

As C++ preserve order of operand. Compiler resolve above addition call as 5.operator+ (const Complex & other);// which is not possible Hence, overload it via free function.

Your class is exposing necessary information via public interface such as aGetValue() and bGetValue. Hence, this free overloaded + operator function need not be friend of class.

Additionally, Prefer non friend non member function over member function as it helps reduce degree of encapsulation. This is explained here ==> http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197?pgno=1

Comments

0

You can declare a friend to your Complex class

class Complex { // blah.... friend Complex operator+(Complex const& a, Complex const & b); }; 

The overloaded operator can access the private members of Complex.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.