Question 6
Back to Exercise
What is the output of the following C++ program?
#include <iostream>
class Point {
const int x_, y_;
public:
Point(int x, int y) : x_(x), y_(y) {}
Point with_x(int x) const { return {x, y_}; }
Point moved(int dx, int dy) const { return {x_ + dx, y_ + dy}; }
void print() const { std::cout << '(' << x_ << ',' << y_ << ") "; }
};
int main() {
const Point origin(0, 0);
Point p = origin.moved(2, 3).with_x(10);
origin.print();
p.print();
std::cout << '\n';
return 0;
}
- A (0,0) (10,3) Correct
- B (10,3) (10,3)
- C (2,3) (10,3)
- D (0,0) (12,3)
Correct Answer: Option A
Each operation returns a new Point; origin never changes. moved gives (2, 3), with_x replaces x: (10, 3).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!