Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please explain why answer is 4 class Complex{ friend Complex add(Complex, Comple

ID: 3829419 • Letter: P

Question

please explain why answer is 4

class Complex{
friend Complex add(Complex, Complex);
private: double rl, im;
public:
Complex(double r, double i): rl(r), im(i) {cout << "new ";}
// copy constructor
Complex(const Complex & oth): rl(oth.rl), im(oth.im) {cout << "copy ";}
};
Complex add(Complex x, Complex y){ return Complex(x.rl + y.rl, x.im +y.im); }
int main(void){
Complex c1(2, 3);
Complex c2(0, -4.5)
Complex c3 = c1;
Complex c4(c2);
Complex c5 = add(c3, c4);
}
How many times will "copy" be displayed when this code is executed?
a) 2 b) 3 c) 4 d) 5 e) 6

Explanation / Answer

Answer: c) 4

Four times will "copy" be displayed when this code is executed.

Complex c3 = c1;
Complex c4(c2);

When above two statements execute, copy will display and also when we execute Complex c5 = add(c3, c4); this statement two times again copy will display. So totally 4 times it will display.