Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
#include "point.h"
2
3
4
Point::Point()
5
{
6
this->_x = this->_y = 0;
7
}
8
9
Point::Point(double _x, double _y)
10
{
11
this->_x = _x;
12
this->_y = _y;
13
}
14
15
//overloaded methods for accessing x and y
16
double
17
Point::x()
18
{
19
return _x;
20
}
21
22
void
23
Point::x(double val)
24
{
25
_x = val;
26
}
27
28
double
29
Point::y()
30
{
31
return _y;
32
}
33
34
void
35
Point::y(double val)
36
{
37
_y = val;
38
}
39
40
41
//overloaded addition and subtraction operators
42
Point
43
Point::operator+(Point &rhs)
44
{
45
Point result;
46
47
result._x = _x + rhs._x;
48
result._y = _y + rhs._y;
49
50
return result;
51
}
52
53
Point &
54
Point::operator+=(Point &rhs)
55
{
56
_x += rhs._x;
57
_y += rhs._y;
58
59
return *this; //return a reference to me
60
}
61
62
Point
63
Point::operator-(Point &rhs)
64
{
65
Point result;
66
67
result._x = _x - rhs._x;
68
result._y = _y - rhs._y;
69
70
return result;
71
72
}
73
74
Point &
75
Point::operator-=(Point &rhs)
76
{
77
_x -= rhs._x;
78
_y -= rhs._y;
79
80
return *this; //return a reference to me
81
}
82
83
84
std::ostream &
85
operator<<(std::ostream &os, Point &rhs)
86
{
87
//write to the stream
88
os << "(" << rhs.x() << ", " << rhs.y() << ")";
89
90
//return the stream
91
return os;
92
}
93
94
95