operator overloading | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

operator overloading

I'm doing an exercise that asks to create a Complex class and to implement sum and sub operator as well as the == operator. I'm confident with the first two, but I can't say the same for the latter. Also, what would you answer to the question: does the class need an assignment operator and a deep copy operator? class Complex { public: Complex():re (0), im(0){}; Complex(double r, double i): re(r), im(i){}; bool operator==(const Complex& right) { bool result = true; if(this != &right){ result = false; } return result; } Complex operator+(const Complex& op) { return Complex(re + op.re, im + op.im); } Complex operator-(const Complex& op){ return Complex(re - op.re, im - op.im); } private: double re; double im; };

15th Jul 2019, 8:21 AM
Frangiax
Frangiax - avatar
3 Answers
+ 3
Thank you guys!
15th Jul 2019, 11:01 AM
Frangiax
Frangiax - avatar
+ 1
the implementation of "Complex::operator==" will be something like this ( since both the real and imaginary parts need to be equal): https://code.sololearn.com/cWKod0HB0uWD/#cpp you don't need to create a copy constructor and assignment operator, C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects if it is used and it’s deleted if a move operation is declared, but you need to define your own only if the object has pointers or any runtime allocation of the resource.
15th Jul 2019, 10:51 AM
MO ELomari
0
... Anyone?
15th Jul 2019, 9:49 AM
Frangiax
Frangiax - avatar