一个三维点类Gpoint3的实现

时间:2022-12-27 16:16:56

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时出现无法解析的外部符号;

  一个三维点类Gpoint3的实现

  一个三维点类Gpoint3的实现

  • 重载ostream问题

  出现非法使用显示模版参数的错误,正确定义为operator<< <>,不需要尖括号中的T

  一个三维点类Gpoint3的实现

  一个三维点类Gpoint3的实现

  • 定义别名,增强编程的可阅读性
typedef Gpoint3<int>       Gpoint3i;
typedef Gpoint3<float> Gpoint3f;
typedef Gpoint3<double> Gpoint3d;

3 Gpoint3模板类实现

 ////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017, DuanBeiChen, all rights reserved.
//
// Author : DuanBeiChen
// Create : 2017-12-10 09:21
// Mail : duanchristian2015@gmail.com
// Version : 1.0
//
//
// Description : 一个基本的三维点类
// (1)默认构造时,自动初始化为(0,0,0);
// (2)支持点之间的加、减运算, point3 = point1 + point2;
// (3)支持点与常量数据的加、减、乘除运算, point2 = point1 + x;
// (4)支持点之间的相等或不能判断;
// (5)如果把点类看作一个向量,应该支持向量的点乘、求模操作;
////////////////////////////////////////////////////////////////////////////////////////
#pragma once #ifndef _G_POINT3_H_
#define _G_POINT3_H_ #include <iostream>
namespace DB
{ template <typename T> class Gpoint3
{
public:
//default constructor
Gpoint3(); //copy constructor
Gpoint3(const Gpoint3 &point1);
Gpoint3<T>& operator = (const Gpoint3<T> &point1); //constructor
Gpoint3(T m_x,T m_y,T m_z); ~Gpoint3(); /* 重载运算符 */
void operator += (const Gpoint3 &point1);
void operator -= (const Gpoint3 &point1);
void operator += (const T value);
void operator -= (const T value);
void operator *= (const T value);
void operator /= (const T value); bool operator == (const Gpoint3 &point1);
bool operator != (const Gpoint3 &point1); /* 重载操作流 ,可以删除声明 */
template <typename T>
friend std::ostream& operator<< <> (std::ostream &out, const Gpoint3<T> &point1); /* 友元函数 ,这部分声明可以删除 */
template <typename T>
friend Gpoint3<T> operator+ (const Gpoint3<T> &point1,const Gpoint3<T> &point2);
template <typename T>
friend Gpoint3<T> operator- (const Gpoint3<T> &point1,const Gpoint3<T> &point2);
template <typename T>
friend Gpoint3<T> operator+ (const T value ,const Gpoint3<T> &point2);
template <typename T>
friend Gpoint3<T> operator- (const T value ,const Gpoint3<T> &point2);
template <typename T>
friend Gpoint3<T> operator* (const T value ,const Gpoint3<T> &point2);
template <typename T>
friend Gpoint3<T> operator+ (const Gpoint3<T> &point1,const T value);
template <typename T>
friend Gpoint3<T> operator- (const Gpoint3<T> &point1,const T value);
template <typename T>
friend Gpoint3<T> operator* (const Gpoint3<T> &point1,const T value); public:
T x;
T y;
T z; }; /************************************************************************/
/* 构造函数和析构函数
/************************************************************************/
template <typename T>
DB::Gpoint3<T>::Gpoint3(): x() ,y() ,z() { } template <typename T>
inline DB::Gpoint3<T>::Gpoint3(const Gpoint3 &point1) : x(point1.x) , y(point1.y) ,z(point1.z){ } template <typename T>
inline DB::Gpoint3<T>::Gpoint3( T m_x,T m_y,T m_z ): x(m_x), y(m_y), z(m_z){ } template <typename T>
inline Gpoint3<T>& DB::Gpoint3<T>::operator=( const Gpoint3<T> &point1 )
{
x = point1.x;
y = point1.y;
z = point1.z; return *this;
} template <typename T>
DB::Gpoint3<T>::~Gpoint3()
{
x = ;
y = ;
z = ;
} /************************************************************************/
/* 重载运算符
/************************************************************************/
template <typename T>
inline void DB::Gpoint3<T>::operator+=( const Gpoint3 &point1 )
{
x += point1.x;
y += point1.y;
z += point1.z;
} template <typename T>
inline void DB::Gpoint3<T>::operator-=( const Gpoint3 &point1 )
{
x -= point1.x;
y -= point1.y;
z -= point1.z;
} template <typename T>
inline void DB::Gpoint3<T>::operator+=(const T value)
{
x += value;
y += value;
z += value;
} template <typename T>
inline void DB::Gpoint3<T>::operator-=(const T value)
{
x -= value;
y -= value;
z -= value;
} template < typename T>
inline void DB::Gpoint3<T>::operator*= ( const T value)
{
x *= value;
y *= value;
z *= value;
} template < typename T>
inline void DB::Gpoint3<T>::operator/= ( const T value)
{
if(abs(value) > 1e-)
{
x /= value;
y /= value;
z /= value;
}
} template <typename T>
inline bool DB::Gpoint3<T>::operator== (const Gpoint3 &point1)
{
return (x == point1.x && y == point1.y && z == point1.z);
} template <typename T>
inline bool DB::Gpoint3<T>::operator!= (const Gpoint3 &point1)
{
return !(x == point1.x && y == point1.y && z == point1.z);
} /************************************************************************/
/* Gpoint3 : non - member function
/************************************************************************/
template <typename T>
inline Gpoint3<T> operator+ (const Gpoint3<T> &point1, const Gpoint3<T> &point2)
{
Gpoint3<T> tempPoint;
tempPoint.x = point1.x + point2.x;
tempPoint.y = point1.y + point2.y;
tempPoint.z = point1.z + point2.z; return tempPoint;
} template <typename T>
inline Gpoint3<T> operator- (const Gpoint3<T> &point1, const Gpoint3<T> &point2)
{
Gpoint3<T> tempPoint;
tempPoint.x = point1.x - point2.x;
tempPoint.y = point1.y - point2.y;
tempPoint.z = point1.z - point2.z; return tempPoint;
} template <typename T>
inline Gpoint3<T> operator+ (const Gpoint3<T> &point1 , const T value)
{
Gpoint3<T> tempPoint;
tempPoint.x = point1.x + value;
tempPoint.y = point1.y + value;
tempPoint.z = point1.z + value; return tempPoint;
} template <typename T>
inline Gpoint3<T> operator- (const Gpoint3<T> &point1 , const T value)
{
Gpoint3<T> tempPoint;
tempPoint.x = point1.x - value;
tempPoint.y = point1.y - value;
tempPoint.z = point1.z - value; return tempPoint;
} template <typename T>
inline Gpoint3<T> operator* (const Gpoint3<T> &point1 , const T value)
{
Gpoint3<T> tempPoint;
tempPoint.x = point1.x * value;
tempPoint.y = point1.y * value;
tempPoint.z = point1.z * value; return tempPoint;
} template <typename T>
inline Gpoint3<T> operator/ (const Gpoint3<T> &point1 , const T value)
{
Gpoint3<T> tempPoint; if(abs(value) > 1e-)
{
tempPoint.x = point1.x / value;
tempPoint.y = point1.y / value;
tempPoint.z = point1.z / value;
} return tempPoint;
} template <typename T>
inline Gpoint3<T> operator+ (const T value, const Gpoint3<T> &point2)
{
return (point2 + value);
} template <typename T>
inline Gpoint3<T> operator- (const T value, const Gpoint3<T> &point2)
{
Gpoint3<T> tempPoint;
tempPoint.x = value - point2.x;
tempPoint.y = value - point2.y;
tempPoint.z = value - point2.z; return tempPoint; } template <typename T>
inline Gpoint3<T> operator* (const T value, const Gpoint3<T> &point2)
{
return (point2 * value);
} /************************************************************************/
/* 文件流
/************************************************************************/
template <typename T>
std::ostream& operator<< <>(std::ostream &out,const Gpoint3<T> &point1)
{
out << point1.x << " " << point1.y << " " << point1.z;
return out;
} typedef Gpoint3<int> Gpoint3i;
typedef Gpoint3<float> Gpoint3f;
typedef Gpoint3<double> Gpoint3d; } #endif

4 模版类测试

 #include "Gpoint3.h"

 int main()
{ DB::Gpoint3<int> point1;
std::cout << " point1 is " << point1 << std::endl; DB::Gpoint3<float> point2(1.0,2.0,3.0);
DB::Gpoint3<float> point3(2.2,3.5,4.1);
point3 += point2;
std::cout << " point2 is " << point2 << std::endl;
std::cout << " point3 is " << point3 << std::endl; point3 *= ;
std::cout << " point3 is " << point3 << std::endl; DB::Gpoint3<float> point4(10.0,10.0,10.0);
point4 /= 1e-;
std::cout << " point4 is " << point4 << std::endl; DB::Gpoint3<double> point5(1.2,1.2,);
DB::Gpoint3<double> point6 = (double) + point5 - (double); point5 = point6; if(point5 == point6)
{
std::cout << " point5 is equal to point6 " << std::endl;
std::cout << point6 << std::endl;
}
else
{
std::cout << " point5 is not equal to point6 " << std::endl;
} }