(Using C++)The type Point is a fairly simple data type, but under another name (
ID: 3739912 • Letter: #
Question
(Using C++)The type Point is a fairly simple data type, but under another name (the template class pair) this data type is defined and used in the C++ Standard Template Library, although you need not know anything about the Standard Template Library to do this exercise. Write a definition of a class named Point that might be used to store and manipulate the location of a point in the plane.
You will need to code, declare, and implement the following member functions:
a. A member function set that sets the private data after an object of this class is created.
b. A member function to move the point by an amount along the vertical and horizontal directions specified by the first and second arguments.
c. A member function to rotate the point by 90 degrees clockwise around the origin.
d. Two const inspector functions to retrieve the current coordinates of the point.
Document these functions with appropriate comments. Embed your class in a test program that behaves as described below.
Your program should maintain 3 Point objects that can be modified. You can use an array of point objects (preferred) or you can declare three individual Point objects
The program should present a menu with the following options. It should accept both upper and lower case letters for the options, and if what is entered is not one of the options it should print ‘That is not a valid option’ and display the menu again. The options are as follows:
s : set the coordinates of point
m: move a point
r : rotate a point
q: quit
The behavior for each function is described below:
s: Ask the user which point they would like to set (1-3). Then ask the user to enter the x and y coordinates, separated by a space. Set the point and print the point number (1-3) followed by the coordinates.
m: ask which Point they would like to move. Print the current Point number and its coordinates. Then ask for the number of units to move in the x and y directions, separated by a space. Print the new coordinates of the point
r: ask which Point they would like to rotate. Print the current Point number and its current coordinates. Then print the new coordinates of the point
q: exit the program
Your output must match the following:
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
e
That is not a valid option.
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
s
Which point would you like to set? (1-3) 4
Please choose a Point between 1 and 3 2
Enter the x and y coordinates, separated by a space: 1 2
Point 2's coordinates are now 1, 2
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
m
Which point would you like to move? (1-3) 1
Point 1's coordinates are 0,0
Enter the amount you would like to move in the x and y direction, followed by a space
-3 4
Point 1's coordinates are now -3, 4
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
M
Which point would you like to move? (1-3) 2
Point 2's coordinates are 1, 2
Enter the amount you would like to move in the x and y direction, followed by a space
-3 4
Point 2's coordinates are now -2, 6
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
r
Which point would you like to rotate? (1-3) 2
Point 2's coordinates are -2, 6 Point 2's coordinates are now 6, 2
Choose from the following options:
s: set a Point's coordinates
m: move a Point
r: rotate a Point
q: quit
Q
Explanation / Answer
Please find my implementation:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(double initial_x = 0, double initial_y = 0);
void shift(double x_amount, double y_amount);
void rotate90();
int rotation_needed(Point p);
void rotation_upper_right(Point &p);
double get_x() const {return x;}
double get_y() const {return y;}
friend istream& operator >>(istream &ins, Point &target);
private:
double x;
double y;
};
//NON-MEMBER FUNCTIONS
double distance(const Point &p1, const Point &p2);
Point mid_point(const Point &p1, const Point &p2);
bool operator ==(const Point &p1, const Point &p2);
bool operator !=(const Point &p1, const Point &p2);
Point operator +(const Point &p1, const Point &p2);
ostream& operator <<(ostream &outs, const Point &source);
Point::Point(double initial_x, double initial_y)
{
x = initial_x;
y = initial_y;
}
void Point::shift(double x_amount, double y_amount)
{
x += x_amount;
y += y_amount;
}
void Point::rotate90()
{
double new_x;
double new_y;
new_x = y;
new_y = -x;
x = new_x;
y = new_y;
}
int Point::rotation_needed(Point p)
{
int answer = 0;
while((p.get_x() < 0) || (p.get_y() < 0))
{
p.rotate90();
answer++;
}
return answer;
}
void Point::rotation_upper_right(Point &p)
{
while((p.get_x() < 0) || (p.get_y() < 0))
{
p.rotate90();
}
}
double distance(const Point &p1,const Point &p2)
{
double a, b, c_square;
a = p1.get_x() - p2.get_x();
b = p1.get_y() - p2.get_y();
c_square = a*a + b*b;
return (sqrt(c_square));
}
Point mid_point(const Point& p1, const Point& p2)
{
double x_midpoint, y_midpoint;
x_midpoint = (p1.get_x() + p2.get_x()) / 2;
y_midpoint = (p1.get_y() + p2.get_y()) / 2;
Point midpoint(x_midpoint, y_midpoint);
//construct new object midpoint with values of x_midpoint and y_midpoint
return midpoint;
}
bool operator ==(const Point &p1, const Point &p2)
{
return ((p1.get_x() == p2.get_x()) && (p1.get_y() == p2.get_y()));
}
bool operator !=(const Point &p1, const Point &p2)
{
return (!(p1 == p2));
}
Point operator +(const Point &p1, const Point &p2)
{
double x_sum, y_sum;
x_sum = p1.get_x() + p2.get_x();
y_sum = p1.get_y() + p2.get_y();
Point sum(x_sum, y_sum);
return sum;
}
ostream& operator <<(ostream &outs, const Point &source)
{
outs << source.get_x() << " " << source.get_y();
return outs;
}
istream& operator >>(istream &ins, Point &target)
{
ins >> target.x >> target.y;
return ins;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.