Question discussion

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

2173Medium DSSSB TGT Computer Science 0 replies 1 views
0

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

#include <iostream>
using namespace std;
class V {
    int x;
public:
    V(int a) : x(a) {}
    V operator+(const V& o) const { return V(x * 10 + o.x); }
    int get() const { return x; }
};
int main() {
    V a(2), b(3);
    cout << (a + b).get() << " " << (a + b + a).get();
    return 0;
}
  • A5 7
  • B32 322
  • C23 25
  • D23 232
Correct answer: D

Explanation

a + b = V(2 × 10 + 3) = 23, and (a + b) + a = V(23 × 10 + 2) = 232 because + associates from the left.

Discussion 0

Nobody has replied yet. Start the discussion!