Using C++, write the following program:
The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators
Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that returns the distance between two points & declarations for overloading the << (output), == (comparison), < (less than), + (plus) and - (minus) operators.)
When comparing two 2D points for this assignment use the dictionary order comparison
For all operators where you have to choose to either implement them as a member function or a non-member function, use the following rules of thumb to decide:
/*
* Point.h
*
*/
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
class Point {
public:
/**
* Constructor and destructor
*/
Point();
Point(const double x, const double y);
virtual ~Point();
/**
* Get the x value
*/
double getX() const;
/**
* Get the y value
*/
double getY() const;
/**
* Return the distance between Points
*/
double slope(const Point &p);
/**
* Output the Point as (x, y) to an output stream
*/
friend std::ostream& operator <<(std::ostream&, const Point&);
/**
* Declare comparison relationships
*/
friend bool operator ==(const Point &lhs, const Point &rhs);
friend bool operator <(const Point &lhs, const Point &rhs);
/**
* Declare math operators
*/
friend Point operator +(const Point &lhs, const Point &rhs);
friend Point operator -(const Point &lhs, const Point &rhs);
private:
double x;
double y;
};
#endif /* POINT_H_ */
Program plan: As per the algorithm is given, the code was written commented clearly. there are some other methods mentioned for implementation like the magnitude of the points, setter methods for point objects.
Mathematcal explanation:
Formulae of slope: take two points p1(x1,y1) ,p2(x2,y2)
then slope=(y2-y1)/(x2-x1)
Magnitude= square roots of (x*x)+(y*y)
Program:
//include directives
#include<math.h>
#include <iostream>
using namespace std;
//point class definition
class point
{
private:
double x, y;
public:
point ()
//default constructor
{
x = 0;
y = 0;
}
point (double a, double b) //parameterised
constructor
{
x = a;
y = b;
}
point (point & p1)
//parameterised constructor with object itself
{
x = p1.x;
y = p1.y;
}
//setter methods for coordinates
void setX (double a)
{
x = a;
}
void setY (double b)
{
y = b;
}
//getter methods for coordinates
double getX ()
{
return x;
}
double getY ()
{
return y;
}
//setter method for coordinates
void setXY (double a, double b)
{
x = a;
y = b;
}
//to get magnitude ogf the point
double getMagnitude ()
{
return (sqrt ((x * x) + (y * y)));
}
/* normal pint function to print the point *
* void print() *
*{ *
* cout<<"\npoint is
("<<x<<","<<y<<")";*
*}*/
//getSlope method to calculate the slope
double getSlope (point & p2)
{
double xd = p2.x - x;
double yd = p2.y - y;
return (yd / xd);
}
//overload functions declared here
friend std::ostream & operator << (std::ostream &,
const point &);
// Declaration of comparison relationships
friend bool operator == (const point & lhs, const point &
rhs);
friend bool operator < (const point & lhs, const point &
rhs);
// Declaration of math operators
friend point operator + (const point & lhs, const point &
rhs);
friend point operator - (const point & lhs, const point &
rhs);
//Destructor definition
~point ()
{
}
};
//End of point class
// Definition of overload output operator
ostream & operator << (std::ostream & out, const
point & p)
{
out << "(" << p.x;
out << "," << p.y << ")" << "\n";
}
//definition of equals operator overlaoding
bool operator == (const point & lhs, const point &
rhs)
{
if ((lhs.x == rhs.x) && (lhs.y == rhs.y))
return 1;
else
return 0;
}
//definition of < operator overloading
bool operator < (const point & lhs, const point &
rhs)
{
if ((lhs.x < rhs.x) && (lhs.y < rhs.y))
return 1;
else
return 0;
}
//definition of overloaded + operator
point operator + (const point & lhs, const point &
rhs)
{
point p;
p.x = lhs.x + rhs.x;
p.y = lhs.y + rhs.y;
return p;
}
//definition of overloaded - operator
point operator - (const point & lhs, const point &
rhs)
{
point p;
p.x = lhs.x + rhs.x;
p.y = lhs.y + rhs.y;
return p;
}
//main method definition
int main ()
{
//point class objects declared by using constructors defined in
class
point p1;
point p2 (2, 3);
point p3 (p2);
point r;
double d;
p2.setXY (4, 5);
//calculating the magnitude
d = p1.getMagnitude ();
cout << "\nmagnitude value of p1 and p2" << d;
//calling the getSlope method
cout << "\nSlope p2 and p3:" << p2.getSlope (p3);
//using overloaded operators to compare the points
if (p1 == p2)
cout << "\np1 and p2 are the same points";
if (p1 < p2)
cout << "\n p1 is lessthan point p2";
//using the math operators to add and subtract point objects
r = p1 + p2;
cout << "Addition of two points are : " << r;
r = p2 - p3;
cout << "Subtraction of two points are : " << r;
//Displaying the points directly by using overloaded extraction
operator
cout << " the points are:" << p1 << p2 <<
p3;
return 0;
}
Sample output:
magnitude value of p1 and p20
Slope p2 and p3:1
p1 is lessthan point p2Addition of two points are : (4,5)
Subtraction of two points are : (6,8)
the points are:(0,0)
(4,5)
(2,3)
output run on TURBO CPP:
Get Answers For Free
Most questions answered within 1 hours.