-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_class.cc
82 lines (70 loc) · 1.42 KB
/
point_class.cc
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
82
/*
Mike Zhong
91.201-Rumshisky
Project1_2, Due: 11/26/15
*/
#include "point_class.h"
using namespace std;
Point::Point(int _dimension)
{
for(int i = 0; i < _dimension; ++i){
coordinates.push_back(0);
}
}
Point::Point(vector<double> _coordinates)
{
coordinates = _coordinates;
}
Point::Point(const Point& rhs)
{
coordinates = rhs.coordinates;
}
Point& Point::operator=(const Point& rhs)
{
this->coordinates = rhs.coordinates;
return *this;
}
Point& Point::operator+=(const Point& rhs)
{
assert(this->coordinates.size() == rhs.coordinates.size());
for(int i = 0; i < rhs.coordinates.size(); ++i){
this->coordinates[i] += rhs.coordinates[i];
}
return *this;
}
double& Point::operator[](const int index)
{
assert(index >=0 && index < this->coordinates.size());
return this->coordinates[index];
}
Point& Point::operator++()
{
for(int i = 0; i < this->coordinates.size(); ++i){
++this->coordinates[i];
}
return *this;
}
Point Point::operator++(int)
{
Point temp = *this;
for(int i = 0; i < this->coordinates.size(); ++i){
++this->coordinates[i];
}
return temp;
}
ostream& operator<<(ostream& os, const Point& x)
{
os << "(";
for(int i = 0; i < x.coordinates.size() - 1; i++){
os << x.coordinates[i] << ",";
}
os << x.coordinates.back() << ")";
return os;
}
Point operator+(const Point& x, const Point& y)
{
assert(x.coordinates.size() == y.coordinates.size());
Point temp = x;
temp += y;
return temp;
}