Question discussion

What is the output of the following C++ program?#include using namespace std; class P { public: int x; P(int a) : x(a) {} P operator+(P o)…

1159Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
using namespace std;
class P {
public:
    int x;
    P(int a) : x(a) {}
    P operator+(P o) { return P(x * 10 + o.x); }
};
int main() {
    P a(1), b(2), c(3);
    cout << (a + b + c).x;
}
  • A6
  • B15
  • C321
  • D123
Correct answer: D

Explanation

a + b = P(12) and then P(12) + c = P(12 × 10 + 3) = P(123), so x is 123.

Discussion 0

Nobody has replied yet. Start the discussion!