Question discussion

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

1747Medium 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 {
public:
    int x, y;
    V(int a, int b) : x(a), y(b) {}
    V operator+(const V& o) const { return V(x + o.x, y * o.y); }
};
int main() {
    V a(1, 2), b(3, 4);
    V c = a + b + a;
    cout << c.x << " " << c.y;
    return 0;
}
  • A5 32
  • B5 8
  • C4 16
  • D5 16
Correct answer: D

Explanation

a + b = (4, 8) because the overloaded operator multiplies the y parts; adding a again gives (5, 16).

Discussion 0

Nobody has replied yet. Start the discussion!