I'm currently trying to set up a program for a Point class in C++, with three constructors, a default one that sets x and y to 0, a one-parameter constructor that takes y from first two digits (of a four digit number) and x from the last two digits, and a two-parameter constructor that takes in an input for x and y.
I have a method for finding distance and the point's quadrant which should work currently.
Now I'm told to overload the > operator to compare the distance from the origin to two Point objects being compared. Thing is, I got no clue how to write an operator overload method, so after searching up Internet tutorials, it told me to follow a format akin to bool operator > (Point const p){, which doesn't work. The compiler I'm using just either doesn't explain the error, or says that it has to do with Point needing to take two arguments.
For context, here's the full code.
#include <stdio.h>
#include <math.h>
class Point{
public:
int x, y;
Point()
{
x = 0;
y = 0;
}
Point (int theX, int theY){
x = theX;
y = theY;
}
Point (int fourDigits){
x = fourDigits % 100;
y = fourDigits / 100;
}
};
int findDistance(Point p){
int d = sqrt(pow(p.x, 2)+pow(p.y, 2));
return d;
}
int findQuadrant(Point p){
int quad;
if(p.x==0&&p.y==0){
quad = 0;
printf("\nOrigin\n");
}
else if(p.x==0&&p.y!=0){
quad = 5;
printf("\nX-axis\n");
}
else if(p.x!=0&&p.y==0){
quad = 6;
printf("\nY-axis\n");
}
else if(p.x<0&&p.y>0){
quad = 2;
printf("\nSecond quadrant\n");
}else if(p.x<0&&p.y<0){
quad = 3;
printf("\nThird quadrant\n");
}else if(p.x>0&&p.y<0){
quad = 4;
printf("\nFourth quadrant\n");
}else{//X>0 and Y>0 assumed to be the default
quad = 1;
printf("\nFirst quadrant\n");
}
return quad;
}
bool operator > (Point const p){//this header gives an error b/c of Point needing two arguments.
//To be frank, I don't know if this is even the right header anyhow.
//this is where i should overload the operators. Not sure how to do it.
}
int main()
{
//work in progress
return 0;
}