Design Patterns
point.h
Go to the documentation of this file.
1 // Based on "Design Patterns: Elements of Reusable Object-Oriented Software"
2 // book by Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm
3 //
4 // Created by Bartosz Rachwal. The National Institute of Advanced Industrial Science and Technology, Japan.
5 
6 #ifndef STRUCTURAL_COMMONS_POINT_H_
7 #define STRUCTURAL_COMMONS_POINT_H_
8 
9 #include <iostream>
10 
11 namespace structural
12 {
13 namespace commons
14 {
15 template<class type>
16 class Point
17 {
18  public:
19  Point();
20  Point(const Point<type>&);
21  Point(const type&, const type&);
22 
23  type x() const;
24  void x(type x);
25  type y() const;
26  void y(type y);
27 
28  friend Point<type> operator+(const Point<type>& left, const Point<type>& right)
29  {
30  Point<type> left1(left);
31  return left1 += right;
32  }
33 
34  friend Point<type> operator-(const Point<type>& left, const Point<type>& right)
35  {
36  Point<type> left1(left);
37  return left1 -= right;
38  }
39 
42  Point<type> &operator*=(const type&);
43  Point<type> &operator/=(const type&);
44 
46 
47  friend bool operator==(const Point<type>& left, const Point<type>& right)
48  {
49  return left.x() == right.x() && left.y() == right.y();
50  }
51 
52  friend bool operator!=(const Point<type>& left, const Point<type>& right)
53  {
54  return !(left == right);
55  }
56 
57  friend std::ostream &operator<<(std::ostream& to, const Point<type>& point)
58  {
59  to << point.x() << " " << point.y();
60  return to;
61  }
62 
63  friend std::istream &operator>>(std::istream& from, Point<type>& point)
64  {
65  return from;
66  }
67 
68  private:
69  type x_;
70  type y_;
71 };
72 
73 template<class type>
74 Point<type>::Point() : Point<type>(0, 0) { }
75 
76 template<class type>
77 Point<type>::Point(const type& x, const type& y) : x_(x), y_(y) { }
78 
79 template<class type>
80 Point<type>::Point(const Point<type>& point) : Point<type>(point.x_, point.y_) { }
81 
82 template<class type>
83 type Point<type>::x() const
84 {
85  return x_;
86 }
87 
88 template<class type>
89 void Point<type>::x(type x)
90 {
91  x_ = x;
92 }
93 
94 template<class type>
95 type Point<type>::y() const
96 {
97  return y_;
98 }
99 
100 template<class type>
101 void Point<type>::y(type y)
102 {
103  y_ = y;
104 }
105 
106 template<class type>
108 {
109  x_ += point.x();
110  y_ += point.y();
111  return *this;
112 }
113 
114 template<class type>
116 {
117  x_ -= point.x();
118  y_ -= point.y();
119  return *this;
120 }
121 
122 template<class type>
124 {
125  x_ *= point.x();
126  y_ *= point.y();
127  return *this;
128 }
129 
130 template<class type>
132 {
133  x_ /= value;
134  y_ /= value;
135  return *this;
136 }
137 
138 template<class type>
140 {
141  x_ = -x_;
142  y_ = -x_;
143  return *this;
144 }
145 }
146 }
147 
148 #endif
149 
Point< type > & operator+=(const Point< type > &)
Definition: point.h:107
Point< type > & operator/=(const type &)
Definition: point.h:131
friend Point< type > operator-(const Point< type > &left, const Point< type > &right)
Definition: point.h:34
type y() const
Definition: point.h:95
Definition: shape_interface.h:11
friend bool operator!=(const Point< type > &left, const Point< type > &right)
Definition: point.h:52
Point< type > & operator*=(const type &)
Definition: point.h:123
Point< type > operator-()
Definition: point.h:139
friend bool operator==(const Point< type > &left, const Point< type > &right)
Definition: point.h:47
Point< type > & operator-=(const Point< type > &)
Definition: point.h:115
Point()
Definition: point.h:74
Definition: point.h:16
friend std::istream & operator>>(std::istream &from, Point< type > &point)
Definition: point.h:63
friend Point< type > operator+(const Point< type > &left, const Point< type > &right)
Definition: point.h:28
type x() const
Definition: point.h:83