Overload Operators

  • + 4 comments

    My solution:

    Complex operator+(const Complex & X, const Complex & Y) {
        Complex Z {X.a + Y.a, X.b + Y.b};
        return Z;
    }
    
    ostream & operator<< (ostream & out, const Complex & X) {
        if (X.b < 0) {
            out << X.a << "-i" << -X.b;
        } else if(X.b > 0) {
            out << X.a << "+i" << X.b;
        } else {
            out << X.a;
        }
        return out;
    }
    

    It wasn't clear what we ought to do in the case that the imaginary part was 0, so I chose to print out only the real part in that case.