Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions