Question discussion

What is the output of the following C++ code involving operator overloading? class Complex { public: int real, imag; Complex(int r, int i)…

559Medium DSSSB TGT Computer Science 0 replies 1 views
0
What is the output of the following C++ code involving operator overloading? class Complex { public: int real, imag; Complex(int r, int i) : real(r), imag(i) {} Complex operator+(const Complex &c) { return Complex(real + c.real, imag + c.imag); } }; int main() { Complex c1(3, 4), c2(1, 2); Complex c3 = c1 + c2; cout << c3.real << " " << c3.imag; return 0; }
  • A3 4
  • B4 6
  • C1 2
  • DCompilation error
Correct answer: B

Discussion 0

Nobody has replied yet. Start the discussion!