码迷,mamicode.com
首页 > 其他好文 > 详细

一个三维点类Gpoint3的实现

时间:2017-12-10 18:30:58      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:增强   names   int   namespace   struct   return   数据类型   operator   测试   

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        : duanchristian2015@gmail.com
  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

 

一个三维点类Gpoint3的实现

标签:增强   names   int   namespace   struct   return   数据类型   operator   测试   

原文地址:http://www.cnblogs.com/BellaVita/p/8017390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!