Question
In C++ Four doubles are read from input, where the first two doubles are the x and y values of point1 and the second two
In C++ Four doubles are read from input, where the first two doubles are the x and y values of point1 and the second two doubles are the x and y values of point2. Define the function to overload the - operator.
Ex: If the input is 13.5 15.0 4.0 4.5, then the output is:
(13.5, 15) - (4, 4.5) = (9.5, 10.5)
Note: The difference of two points is:
the difference of the x values of the points
the difference of the y values of the points
#include
class Point { public: Point(double xCoord = 0.0, double yCoord = 0.0); void Print() const; Point operator-(Point rhs); private: double xCoordinate; double yCoordinate; };
Point::Point(double xCoord, double yCoord) { xCoordinate = xCoord; yCoordinate = yCoord; }
// No need to accommodate for overflow or negative values
// Your Code Goes Here.
void Point::Print() const { cout << xCoordinate << ", " << yCoordinate; }
int main() { double xCoord1; double yCoord1; double xCoord2; double yCoord2; cin >> xCoord1; cin >> yCoord1; cin >> xCoord2; cin >> yCoord2; Point point1(xCoord1, yCoord1); Point point2(xCoord2, yCoord2); Point difference = point1 - point2; cout << "("; point1.Print(); cout << ") - ("; point2.Print(); cout << ") = ("; difference.Print(); cout << ")" << endl; return 0; }
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