Question: add code for a moveBy(int deltaX, int deltaY) function to Segment so that we can shift the segment by some amount in x (deltaX) and
add code for a moveBy(int deltaX, int deltaY) function to Segment so that we can shift the segment by some amount in x (deltaX) and some amount in y (deltaY). Moving a segment should move its endpoints.
Code:
#include
#include
using namespace std;
class Point {
private:
int x = 0;
int y = 0;
public:
Point(int xVal, int yVal) { moveBy(xVal, yVal); }
string toString() { return to_string(x) + ", " + to_string(y); }
void moveBy(int xVal, int yVal) { x += xVal; y += yVal; }
};
class Segment {
public:
string toString() { return p1->toString() + " to " + p2->toString(); }
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
Segment(Point *p1, Point *p2){
this->p1 = p1;
this->p2 = p2;
}
Point *p1;
Point *p2;
//YOUR_CODE_HERE
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
};
int main()
{
Point A(0, 0);
Point B(4, 4);
Segment s1(&A, &B);
cout << s1.toString() << endl;
s1.moveBy(1, 1);
cout << s1.toString() << endl;
s1.moveBy(0, -2);
cout << s1.toString() << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
