一个三维点类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模板类实现
////////////////////////////////////////////////////////////////////////////////////////
// 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;
} }
一个三维点类Gpoint3的实现的更多相关文章
- iOS之02-第一个OC的类
OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 第一个类的源码: /* 人 类名:Person 属性(成员变量\实例变量):体重.年龄 行为 ...
- 怎么使用jquery判断一个元素是否含有一个指定的类(class)
在jQuery中可以使用2种方法来判断一个元素是否包含一个确定的类(class).两种方法有着相同的功能.2种方法如下:(个人喜欢用hasClass()) 1. hasClass( ...
- 我写的一个ExcelHelper通用类,可用于读取或生成数据
读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...
- 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成
写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...
- JAVA 一个特殊的类 Object
一个特殊的类Object:它是java中所有对象的直接或间接父类,根父类(基类),它里面定义的功能是所有对象都应该具备的(所有的类,都是继承这个类的) 记住:当定义一个新类时,没有指明要继承某类,它默 ...
- 【Objective-C】0-第一个OC的类
OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 一.语法简介 1.类 在Java中,我们用1个.java文件就可以描述清楚一个类:在OC中, ...
- 一个使用CSocket类的网络通信实例
http://www.cppblog.com/changshoumeng/archive/2010/05/14/115413.html 3.8 一个使用CSocket类的网络通信实例 本例采用CSoc ...
- 如何创建一个要素数据类 IField,IFieldEdit,IFields,IFieldsEditI,GeometryDef,IGeometryDefEdit接口
如何创建一个要素数据类 创建要素类用到了IFeatureWorkspace.CreateFeatureClass方法,在这个方法中有众多的参数,为了满足这些参数,我们要学习和了解下面的接口. IFie ...
随机推荐
- Java基础总结--IO总结2
1.键盘录入--Java具有特定的对象封装这些输入输出设备在System类定义 in-InputStream类型和out-PrintStream类型成员变量阻塞是方法:read()无数据就阻塞wind ...
- vue.js快速搭建图书管理平台
前 言 上一期简单讲解了vue的基本语法,这一次我们做一个小项目,搭建一个简单的图书管理平台,能够让我们更深刻的理解这门语言的妙用. 1.DEMO样式 首先我们需要搭建一个简单的demo样式 ...
- [http服务]
[http服务] CentOS 6 httpd 程序环境 记录了httpd的主进程编号: v 主程序文件: /usr/sbin/httpd /usr/sbin/httpd.worker /usr/sb ...
- [PGM] Bayes Network and Conditional Independence
2 - 1 - Semantics & Factorization 2 - 2 - Reasoning Patterns 2 - 3 - Flow of Probabilistic Influ ...
- HDU1005 Number Sequence (奇技淫巧模拟)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...
- replace to
要注意的是:插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据. MySQL replace into 有三种形式: 1. repla ...
- 通用的contain函数
用来检测节点所属关系:document.documentElement.contains(document.body) function contains(refNode, otherNode) {i ...
- 关于viewports 设备像素比 密度
首先追溯到像素,第一个麻烦事像素的总量问题,同样的大小的屏幕像素可以差很远,像素大小更小的导致内容也变小 在小屏幕上如果展示巨大的桌面网页,诺基亚的做法是首先载入完整的桌面网页,然后缩放至设备屏幕 ...
- javascript 二维(多维)数组的复制问题
最近在项目中遇到一个动画暂停的效果,需要在动画停止的时候检测当前坐标和已经运行的时间,从而调节时间轴为再次运行时加速. 但是在数组保存方面折腾了半天. var orbitArray = [], lin ...
- 面向亿万级用户的QQ一般做什么?——兴趣部落的 Web 同构直出分享
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:李强,腾讯web开发工程师商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处.原文链接:http://wetest.qq.co ...