Question from the C++ - Fundamentals test

Write a C++ program to add two complex numbers.

Easy

What does the following code return ?

class Complex {
public:
    Complex(double x, double y) : x(x), y(y) {}
    double getX() const {return x;}
    double getY() const {return y;}
    Complex operator+(Complex z2);
private:
    double x;
    double y;
};

Complex Complex::operator+(Complex z2) {
    return Complex(x+z2.getX(), y+z2.getY());
}

int main() {
    Complex z1(1, 1);
    Complex z2(2, 2);
    Complex z3 = z1+z2;
    cout << z1.getX() << ',' << z1.getY() << endl;
    cout << z2.getX() << ',' << z2.getY() << endl;
    cout << z3.getX() << ',' << z3.getY() << endl;
    return 0;
}
Author: SamuelStatus: PublishedQuestion passed 203 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!