Like Segment Composition Time Default Point Constructor Use Initialization List Trick Make Q43905012
This is just like Segment Composition, but this time, there isno default Point constructor. You will have to use theinitialization list trick to make sure that the compiler knows howto set up p1 and p2.
The code below defines a class Point thatrepresents a point on the coordinate plane. ASegment should connect two points (its endpoints). When we make a segment, we will give the constructor theaddresses of two points.
Finish the Segment class by:
- Adding the member variables p1 and p2 needed to hold two Points(OK to do in the public section).
- Making a constructor that accepts two points (you will have touse an initialization list!)
We are defining all the code inside the class declaration. Thus youdon’t need Segment:: in front of your constructor definition. Thisonly works for simple one file projects like this one. Do not dothis in assignments.
#include <iostream>
#include <string>
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
//YOUR_CODE – constructor and member variables
//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;
B.moveBy(1, 1);
cout << s1.toString() << endl;
}
Expert Answer
Answer to This is just like Segment Composition, but this time, there is no default Point constructor. You will have to use the in…
OR