Question
Friend You are given a Point class, which defines a point on a 2D grid (x, y). The program creates a Point in main based
Friend You are given a Point class, which defines a point on a 2D grid (x, y). The program creates a Point in main based on user input and calls the shift() function, which should increment the coordinates by the given step. However, the code is not working, as the coordinates are private. Modify the code to fix it. Remember, functions declared as friend in a class are able to modify the private members.
#include
using namespace std;
class Point {
public:
Point(int a, int b): x(a), y(b) {};
void print() {
cout << x << ", " << y;
}
private:
int x;
int y;
};
void shift(Point &p, int step) {
p.x += step;
p.y += step;
}
int main() {
int x, y;
cin>>x>>y;
Point p(x, y);
int step;
cin>>step;
shift(p, step);
p.print();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started