/* This is a unique program that uses class, and dynamic array, and operator overloading
It allows the user to enter however many points they want
At the end, it will ask the user to enter a target point
Then it will calculate the distance between the points
That are different from the target point
*
*/
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
/* Here we declare the class members and constructors
*
*/
class Point
{
public:
Point()
{
x = 0;
y = 0;
}
void getPoints();
double getX();
double getY();
double getTX();
double getTY();
int getSize();
friend ostream& operator<<(ostream&, Point&);
friend istream& operator>>(istream&, Point&);
private:
double x , y,tx,ty;
int size;
double *xv, *yv;
};
// Here we get the x-coordinate
double Point::getX()
{
return x;
}
// Here we get the y-coordinate
double Point::getY()
{
return y;
}
//Here we get the target x-coordinate
double Point::getTX()
{
return tx;
}
//Here we get the target y-coordinate
double Point::getTY()
{
return ty;
}
//Here we get the number of points to be entered
int Point::getSize()
{
return size;
}
//Here we store the points entered into an array
void Point::getPoints()
{
Point p;
xv = new double[size];
yv = new double[size];
for(int i=0; i<size; i++)
{
cout<<"Point "<<i+1<<": ";
cin>>xv[i]>>yv[i];
}
}
//Overloading the << binary operator
ostream& operator<<(ostream& out, Point& p1)
{
double dd=0.0;
out<<"\n";
for(int j=0; j < p1.size; j++)
{
if((p1.xv[j] == p1.tx) && (p1.yv[j]==p1.ty))
{
out<<"Point("<<p1.tx<<", "<<p1.ty<<")is a target point!\n";
}
else
{
double xd = p1.tx - p1.xv[j];
double yd = p1.ty - p1.yv[j];
dd = sqrt(xd*xd + yd*yd);
out<<"Point " <<"("<<p1.xv[j]<<", "<<p1.yv[j]<<")"
<<"is not a target point, the distance from ("
<<p1.xv[j]<<", "<<p1.yv[j]<<")"<<"to"<<"("
<<p1.tx<<", "<<p1.ty<<"): " <<setprecision(2)<<dd<<"\n";
dd=0.0;
}
}
return out;
}
//Overloading the >> binary operatory
istream& operator>>(istream& in, Point& p)
{
cout<<"Please enter the number of points to be entered\n";
in>>p.size;
cout<<"\nPlease enter the points one by one with x-coordinator first, then y-coordinator\n";
p.getPoints();
cout<<"Please enter a target point\n";
cout<<"Target point: ";
in>>p.tx;
in>>p.ty;
return in;
}
//The main calls the Point class
int main()
{
Point p;
cin>>p;
cout<<p;
system("PAUSE");
return 0;
}
Here is the output of the program
Please enter the number of points to be entered
3
Please enter the points one by one with x-coordinator first, then y-coordinator
Point 1: 3 5
Point 2: -4 6
Point 3: 0 1
Please enter a target point
Target point: 0 1
Point (3, 5)is not a target point, the distance from (3, 5)to(0, 1): 5
Point (-4, 6)is not a target point, the distance from (-4, 6)to(0, 1): 6.4
Point(0, 1)is a target point!
Press any key to continue . . .