1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <iostream>
#include <iomanip>
using namespace std;
class Point
{
public:
double p_x, p_y;
Point(double x=0.0, double y=0.0): p_x(x), p_y(y)
{
}
friend ostream& operator<< (ostream &out, const Point &point);
friend Point operator+(const Point &p1, Point &p2);
friend Point operator-(const Point &p1, Point &p2);
double getX()
{
return p_x;
}
double getY()
{
return p_y;
}
};
ostream& operator<< (ostream &out, const Point &point)
{
out << "Point(" << point.p_x << ", " << point.p_y << ")";
return out;
}
Point operator+(const Point &p1, Point &p2)
{
return Point(p1.p_x + p2.p_x);
return Point(p1.p_y + p2.p_y);
}
/*Point operator+(Point &pY1, Point &pY2)
{
return Point(pY1.p_y + pY2.p_y);
}
*/
Point operator-(const Point &p1, Point &p2)
{
return Point(p1.p_x - p2.p_x);
return Point(p1.p_y - p2.p_y);
}
/*Point operator-(Point &pY1, Point &pY2)
{
return Point(pY1.p_y - pY2.p_y);
}
*/
int main()
{
setprecision(2);
Point point1(2.0, 3.5);
Point point2(6.0, 7.5);
Point addPoint_x = point1.getX() + point2.getX();
Point addPoint_y = point1.getY() + point2.getY();
// Point subPoint_x = point1.getX() - point2.getX();
// Point subPoint_y = point1.getY() - point2.getY();
cout << "Point 1: " << point1 << "\n" << "Point 2: " << point2 << '\n';
cout << "Both points added together become" << endl;
cout << addPoint_x << addPoint_y << '\n';
// cout << "Both points subbed together become" << endl;
// cout << subPoint_x << subPoint_y << '\n';
return 0;
}
|