#include #include "complex.h" using namespace std; void Complex::print()const{ cout << r << "+" << i << "i"; // } void Complex::add(Complex &n){ r += n.r; i += n.i; } bool Complex::isEqualTo(Complex &n)const{ if(r != n.r) return false; // Dangerous if(i != n.i) return false; // fp comparison return true; } bool Complex::operator==(Complex &n)const{ return (*this).isEqualTo(n); // return this->isEqualTo(n); // this would work too // return isEqualTo(n); // this would also work } void Complex::operator+=(Complex &n){ r += n.r; i += n.i; } const Complex Complex::operator+(Complex &n){ Complex temp; temp.r = r + n.r; temp.i = i + n.i; return temp; } ostream &operator<<(ostream &os, Complex &c){ os << c.r << "+" << c.i << "i"; return os; } // for testing main(){ Complex c1(3,2); Complex c2(5,1); Complex c3; cout << "c1:" << c1 << endl; cout << "c2:" << c2 << endl; }