Write the code for a class named Point to represent a point in the Cc rtesian pl
ID: 3749062 • Letter: W
Question
Write the code for a class named Point to represent a point in the Cc rtesian plane with x and y coordinates. The class contains . Two instance variables x and y that represent the coordinates with getX0 and getYO method e A constructor that constructs a point with specified coordinates, with default values 0 and 0 . An str method to return a string representation in the form (x, y) . A method named distance that returns the distance from this point to another point. The formula for finding the distance between » A method named originDistance that returns the distance from this point to the point of origin (0,0). the two points (xl.y1) and (x2, y2) is math sqrt(x2- xl)? + (y2-yl)2) Appropriate code so that points can be compared using the comparison operators. Points are compared based on their distance from the origin Add a test program that does the following e Creates a point object, named pO, that uses the default values for the coordinates. Print p . Creates a point object, named pl, with coordnates at (3,4) Print pl ·Creates a pont ob ect, named p2, with coordinates at 3,0 When creating the o ect, take advantage of the act that the value ofthe v ccordinate s the default value of the corresponding parameter. e Print the x and y coordinates of p2 using the getXO and getYO methods e Find and print the distance between pl and p2 Print the results of comparing pl and p2. Make sure to test all 6 comparison operators. . Your code should allow using and ! to compare a Point object with an object of a different type. Print the result of using the equality and inequality operators to compare pl with the string "Hello"Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
//point class make
class Point
{ //define private member of class for point
int x,y;
public:
//initialse x ,y using Default constructor
Point()
{
this->x=0;
this->y=0;
}
//initialse x with y Default value using parameterized constructor
Point(int x)
{
this->x=x;
this->y=0;
}
//initialse x ,y using parameterized constructor
Point(int x,int y)
{
this->x=x;
this->y=y;
}
//return private member varible X
int getX()
{
return x;
}
//return private member varible Y
int getY()
{
return y;
}
//calculate distance Between Two Points
float distance(Point p1)
{ // initialse Point with current object
Point p2(x,y);
//(x1-x2)2+(y1-y2)2 in temp
double temp=pow((p1.x-p2.x),2) +pow((p1.y-p2.y),2);
//sqrt of temp for distance
float dist= sqrt(temp);
//return distance
return dist;
}
// calculate distance from origin
float originDistance(Point p1)
{
//Point p2 initialse with Default value
Point p2;
//(x1-x2)2+(y1-y2)2 in temp
double temp=pow((p1.x-p2.x),2) +pow((p1.y-p2.y),2);
//sqrt of temp for distance
float dist= sqrt(temp);
//return distance
return dist;
}
// Represenatation of point in string
string _str_()
{ Point p1(x,y);
string s1("(");
string s2=to_string(p1.x);
string s3(",");
string s4=to_string(p1.y);
string s5(")");
s1+=s2;
s1+=s3;
s1+=s4;
s1+=s5;
return s1;
}
// Euqality based on originDistance
bool operator==(Point p1)
{
/* if(p1!=p2)
return false;
else*/
{
Point p2(x,y) ;
return(originDistance(p1)==originDistance(p2));
}
// return false;
}
// Non Euqality based on originDistance
bool operator!=(Point p1)
{
Point p2(x,y) ;
return(originDistance(p1)!=originDistance(p2));
}
// < based on originDistance
bool operator <(Point p1)
{
Point p2(x,y) ;
return(originDistance(p1)<originDistance(p2));
}
// > based on originDistance
bool operator >(Point p1)
{
Point p2(x,y) ;
return(originDistance(p1)>originDistance(p2));
}
// >= based on originDistance
bool operator >=(Point p1)
{
Point p2(x,y) ;
return(originDistance(p1)>=originDistance(p2));
}
// <= based on originDistance
bool operator <=(Point p1)
{
Point p2(x,y) ;
return(originDistance(p1)<=originDistance(p2));
}
};
int main()
{ // created object of Point class with Default value
Point p0;
bool result;
// show p0 value
cout<<"Point p0 with Default value :("<<p0.getX()<<","<<p0.getY()<<") "<<endl;
//created p1 object
Point p1(3,4);
// show p1 value
cout<<"Point p1 is :("<<p1.getX()<<","<<p1.getY()<<") "<<endl;
// created p2 object
Point p2(3);
// show p2 value
cout<<"Point p2 is :("<<p2.getX()<<","<<p2.getY()<<") "<<endl;
//calculate distance b p1 and p2
float dist=p1.distance(p2);
//show distance btn p1 and p2
cout<<"Distance btn Point p1 and Point p2 is :"<<dist<<endl<<endl;
// all six comparision operator are called and show results
result=p1==p2;
cout<<"Euqality(==) operator result Btn p1 and p2 (p1==p2) is :"<<result<<endl;
result=p1!=p2;
cout<<"Not Euality(!=) operator result Btn p1 and p2 (p1!=p2) is :"<<result<<endl;
result=p1>p2;
cout<<"Greater Than(>) operator result Btn p1 and p2 (p1>p2) is :"<<result<<endl;
result=p1<p2;
cout<<"Less Than (<) operator result Btn p1 and p2 (p1<p2) is :"<<result<<endl;
result=p1>=p2;
cout<<"Greater Than Equal To (>=) operator result Btn p1 and p2 (p1>=p2) is :"<<result<<endl;
result=p1<=p2;
cout<<"Less Than Equal To (<=) operator result Btn p1 and p2 (p1<=p2) is :"<<result<<endl;
cout<<endl;
//string Represenatation of point p1
cout<<"string Represenatation of point p1 using _str_ Fn:" <<p1._str_()<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.