一個三維點類Gpoint3的實現

1 類設計

  •   基本功能

  (1)默認構造時,自動初始化爲(0,0,0);

  (2)支持點之間的加、減運算;

  (3)支持點與常量數據的加、減、乘除運算;

  (4)支持點之間的相等或不能判斷

  (5)如果把點類看作一個向量,應該支持向量的點乘、求模操作;

  • 成員變量是採用private還是public屬性

  這是一個操作非常頻繁的類,如果成員變量採用隱藏起來的方式,採用x()取值、採用setX()設置值,用起來很是不爽。

  示例1:point.x = point1.x + point2.x;  (簡單易懂)

  示例2:  point.setX(point1.x() + point2.x()); (大量點運算時,很不爽)

  • 採用普通類還是模版類

  點類計算主要是一些基礎數據類型運算,主要是int、double、float;

  如果採用普通類的話,最好定義默認的數據類型爲double,所有的點運算都按照雙精度浮點型計算,這基本上滿足了很大部分的計算需求;

  儘管如此,本文還是採用模版類以滿足通用性;

2 模版類定義

  非模版類和類成員函數的聲明在h文件中,實現在cpp文件中;但是模版類的聲明和實現都在cpp中。

  • 友元函數聲明問題點

  重載符號operator- 時,聲明前缺少template <typename T>; 調用point = point1 - value時出現無法解析的外部符號;

  

  

  • 重載ostream問題

  出現非法使用顯示模版參數的錯誤,正確定義爲operator<< <>,不需要尖括號中的T

  

  

  • 定義別名,增強編程的可閱讀性
typedef Gpoint3<int>       Gpoint3i;
typedef Gpoint3<float>     Gpoint3f;
typedef Gpoint3<double>    Gpoint3d;

3 Gpoint3模板類實現

  1 ////////////////////////////////////////////////////////////////////////////////////////
  2 // Copyright (C) 2017, DuanBeiChen, all rights reserved.
  3 //
  4 // Author    : DuanBeiChen
  5 // Create    : 2017-12-10 09:21
  6 // Mail        : [email protected]
  7 // Version    : 1.0
  8 //
  9 //
 10 // Description    : 一個基本的三維點類
 11 //                  (1)默認構造時,自動初始化爲(0,0,0);
 12 //                  (2)支持點之間的加、減運算, point3 = point1 + point2;
 13 //                  (3)支持點與常量數據的加、減、乘除運算, point2 = point1 + x;
 14 //                  (4)支持點之間的相等或不能判斷;
 15 //                  (5)如果把點類看作一個向量,應該支持向量的點乘、求模操作;
 16 ////////////////////////////////////////////////////////////////////////////////////////
 17 #pragma once
 18 
 19 #ifndef _G_POINT3_H_
 20 #define _G_POINT3_H_
 21 
 22 #include <iostream>
 23 namespace DB
 24 {
 25     
 26     template <typename T>  class Gpoint3
 27     {
 28     public:
 29         //default constructor
 30         Gpoint3();
 31 
 32         //copy constructor
 33         Gpoint3(const Gpoint3 &point1);
 34         Gpoint3<T>& operator = (const Gpoint3<T> &point1);
 35 
 36         //constructor
 37         Gpoint3(T m_x,T m_y,T m_z);
 38 
 39         ~Gpoint3();
 40 
 41 
 42         /* 重載運算符 */
 43         void operator += (const Gpoint3 &point1);                
 44         void operator -= (const Gpoint3 &point1);
 45         void operator += (const T value);                        
 46         void operator -= (const T value);
 47         void operator *= (const T value);
 48         void operator /= (const T value);
 49 
 50         bool operator == (const Gpoint3 &point1);
 51         bool operator != (const Gpoint3 &point1);
 52 
 53 
 54         /* 重載操作流 ,可以刪除聲明 */
 55         template <typename T> 
 56         friend std::ostream& operator<< <> (std::ostream &out, const Gpoint3<T> &point1);
 57 
 58         /* 友元函數 ,這部分聲明可以刪除 */
 59         template <typename T>
 60         friend Gpoint3<T> operator+ (const Gpoint3<T> &point1,const Gpoint3<T> &point2);
 61         template <typename T> 
 62         friend Gpoint3<T> operator- (const Gpoint3<T> &point1,const Gpoint3<T> &point2);
 63         template <typename T>
 64         friend Gpoint3<T> operator+ (const T value             ,const Gpoint3<T> &point2);
 65         template <typename T>
 66         friend Gpoint3<T> operator- (const T value             ,const Gpoint3<T> &point2);
 67         template <typename T>
 68         friend Gpoint3<T> operator* (const T value             ,const Gpoint3<T> &point2);
 69         template <typename T>
 70         friend Gpoint3<T> operator+ (const Gpoint3<T> &point1,const T value);
 71         template <typename T>
 72         friend Gpoint3<T> operator- (const Gpoint3<T> &point1,const T value);
 73         template <typename T>
 74         friend Gpoint3<T> operator* (const Gpoint3<T> &point1,const T value);
 75 
 76     public:
 77         T x;
 78         T y;
 79         T z;
 80 
 81     };
 82 
 83 
 84     /************************************************************************/
 85     /*  構造函數和析構函數
 86     /************************************************************************/
 87     template <typename T>
 88     DB::Gpoint3<T>::Gpoint3(): x(0) ,y(0) ,z(0) { }
 89 
 90     template <typename T>
 91     inline DB::Gpoint3<T>::Gpoint3(const Gpoint3 &point1) : x(point1.x) , y(point1.y) ,z(point1.z){ }
 92 
 93     template <typename T>
 94     inline DB::Gpoint3<T>::Gpoint3( T m_x,T m_y,T m_z ): x(m_x), y(m_y), z(m_z){ }
 95 
 96     template <typename T>
 97     inline Gpoint3<T>& DB::Gpoint3<T>::operator=( const Gpoint3<T> &point1 )
 98     {
 99         x = point1.x;
100         y = point1.y;
101         z = point1.z;
102 
103         return *this;
104     }
105 
106     template <typename T>
107     DB::Gpoint3<T>::~Gpoint3()
108     {
109         x = 0;
110         y = 0;
111         z = 0;
112     }
113 
114     /************************************************************************/
115     /* 重載運算符
116     /************************************************************************/
117     template <typename T>
118     inline void DB::Gpoint3<T>::operator+=( const Gpoint3 &point1 )
119     {
120         x += point1.x;
121         y += point1.y;
122         z += point1.z;
123     }
124 
125     template <typename T>
126     inline void DB::Gpoint3<T>::operator-=( const Gpoint3 &point1 )
127     {
128         x -= point1.x;
129         y -= point1.y;
130         z -= point1.z;
131     }
132 
133     template <typename T>
134     inline void DB::Gpoint3<T>::operator+=(const T value)
135     {
136         x += value;
137         y += value;
138         z += value;
139     }
140 
141     template <typename T>
142     inline void DB::Gpoint3<T>::operator-=(const T value)
143     {
144         x -= value;
145         y -= value;
146         z -= value;
147     }
148 
149     template < typename T>
150     inline void DB::Gpoint3<T>::operator*= ( const T value)
151     {
152         x *= value;
153         y *= value;
154         z *= value;
155     }
156 
157     template < typename T>
158     inline void DB::Gpoint3<T>::operator/= ( const T value)
159     {
160         if(abs(value) > 1e-7)
161         {
162             x /= value;
163             y /= value;
164             z /= value;
165         }
166     }
167 
168     template <typename T>
169     inline bool DB::Gpoint3<T>::operator== (const Gpoint3 &point1)
170     {
171         return (x == point1.x && y == point1.y && z == point1.z);
172     }
173 
174     template <typename T>
175     inline bool DB::Gpoint3<T>::operator!= (const Gpoint3 &point1)
176     {
177         return !(x == point1.x && y == point1.y && z == point1.z);
178     }
179 
180 
181     /************************************************************************/
182     /* Gpoint3 : non - member function 
183     /************************************************************************/
184     template <typename T>
185     inline Gpoint3<T> operator+ (const Gpoint3<T> &point1, const Gpoint3<T> &point2)
186     {
187         Gpoint3<T> tempPoint;
188         tempPoint.x = point1.x + point2.x;
189         tempPoint.y = point1.y + point2.y;
190         tempPoint.z = point1.z + point2.z;
191 
192         return tempPoint;
193     }
194 
195     template <typename T>
196     inline Gpoint3<T> operator- (const Gpoint3<T> &point1, const Gpoint3<T> &point2)
197     {
198         Gpoint3<T> tempPoint;
199         tempPoint.x = point1.x - point2.x;
200         tempPoint.y = point1.y - point2.y;
201         tempPoint.z = point1.z - point2.z;
202 
203         return tempPoint;
204     }
205 
206     template <typename T>
207     inline Gpoint3<T> operator+ (const Gpoint3<T> &point1 , const T value)
208     {
209         Gpoint3<T> tempPoint;
210         tempPoint.x = point1.x + value;
211         tempPoint.y = point1.y + value;
212         tempPoint.z = point1.z + value;
213 
214         return tempPoint;
215     }
216 
217     template <typename T>
218     inline Gpoint3<T> operator- (const Gpoint3<T> &point1 , const T value)
219     {
220         Gpoint3<T> tempPoint;
221         tempPoint.x = point1.x - value;
222         tempPoint.y = point1.y - value;
223         tempPoint.z = point1.z - value;
224 
225         return tempPoint;
226     }
227 
228     template <typename T>
229     inline Gpoint3<T> operator* (const Gpoint3<T> &point1 , const T value)
230     {
231         Gpoint3<T> tempPoint;
232         tempPoint.x = point1.x * value;
233         tempPoint.y = point1.y * value;
234         tempPoint.z = point1.z * value;
235 
236         return tempPoint;
237     }
238 
239     template <typename T>
240     inline Gpoint3<T> operator/ (const Gpoint3<T> &point1 , const T value)
241     {
242         Gpoint3<T> tempPoint;
243 
244         if(abs(value) > 1e-7)
245         {
246             tempPoint.x = point1.x / value;
247             tempPoint.y = point1.y / value;
248             tempPoint.z = point1.z / value;
249         }
250         
251         return tempPoint;
252     }
253 
254     template <typename T>
255     inline Gpoint3<T> operator+ (const T value, const Gpoint3<T> &point2)
256     {
257         return (point2 + value);
258     }
259 
260     template <typename T>
261     inline Gpoint3<T> operator- (const T value, const Gpoint3<T> &point2)
262     {
263         Gpoint3<T> tempPoint;
264         tempPoint.x = value - point2.x;
265         tempPoint.y = value - point2.y;
266         tempPoint.z = value - point2.z;
267 
268         return tempPoint;
269         
270     }
271 
272     template <typename T>
273     inline Gpoint3<T> operator* (const T value, const Gpoint3<T> &point2)
274     {
275         return (point2 * value);
276     }
277 
278 
279     /************************************************************************/
280     /* 文件流                                                             
281     /************************************************************************/
282     template <typename T>
283     std::ostream& operator<< <>(std::ostream &out,const Gpoint3<T> &point1)
284     {
285         out << point1.x << " " << point1.y << " " << point1.z;
286         return out;
287     }
288 
289     typedef Gpoint3<int>     Gpoint3i;
290     typedef Gpoint3<float>     Gpoint3f;
291     typedef Gpoint3<double>     Gpoint3d;
292 
293 }
294 
295 
296 #endif
View Code

4 模版類測試

 1 #include "Gpoint3.h"
 2 
 3 int main()
 4 {
 5 
 6     DB::Gpoint3<int> point1;
 7     std::cout << " point1 is " << point1 << std::endl;
 8 
 9     DB::Gpoint3<float> point2(1.0,2.0,3.0);
10     DB::Gpoint3<float> point3(2.2,3.5,4.1);
11     point3 += point2;
12     std::cout  << " point2 is " << point2 << std::endl;
13     std::cout  <<  " point3 is " << point3 << std::endl;
14 
15     point3 *= 10;
16     std::cout  <<  " point3 is " << point3 << std::endl;
17 
18     DB::Gpoint3<float> point4(10.0,10.0,10.0);
19     point4 /= 1e-6;
20     std::cout  <<  " point4 is " << point4 << std::endl;
21 
22     DB::Gpoint3<double> point5(1.2,1.2,1);
23     DB::Gpoint3<double> point6 =  (double)5 + point5 - (double)2;
24 
25     point5 = point6;
26 
27     if(point5 == point6)
28     {
29         std::cout  <<  " point5 is equal to point6 " << std::endl;
30         std::cout << point6 << std::endl;
31     }
32     else
33     {
34         std::cout  <<  " point5 is  not equal to point6 " << std::endl;
35     }
36     
37
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章