跌打滚爬中向前的专栏

从没停歇过,因为理想还在远处

3D矢量类CVector3D

写一个3D矢量类CVector3D,要求含三种构造函数,并有拷贝,点积,叉积,求模,单位化等操作,并实现”+; -; =; ==; +=; -=; *; [ ]” 等运算符的重载。

 

注:由于开始定义的时候,没有将三维数据对应的成员变量使用数组存放,因此重载下标运算符'[ ]'没有意义。

 

================================================

 

#include <iostream>
#include <cmath>

using namespace std;

template<class T>
class CVector3D
{
public:
 CVector3D();
 //CVector3D(T x=0,T y=0,T z=0);
 CVector3D(T x,T y,T z);

 ~CVector3D(){};

 //基本运算
 CVector3D(CVector3D &aVector);             //拷贝构造函数
 double DotProduct(CVector3D& aVector);     //向量点积
 CVector3D CrossProduct(CVector3D& aVector);//向量叉积
 CVector3D Normalized();                    //向量单位化
 double VectorModulo();                     //向量模运算
 double VectorAngle(CVector3D& aVector);    //向量夹角
 void VectorPrint();

 //运算符重载
 CVector3D operator +(const CVector3D &ob1);
 CVector3D operator -(const CVector3D &ob1);
 CVector3D& operator =(const CVector3D& src);
 bool operator ==(const CVector3D& obj);
 CVector3D& operator +=(const CVector3D &ob1);
 CVector3D& operator -=(const CVector3D &ob1);
    //CVector3D& operator *(); //指针运算符的重载
 void operator *();         //指针运算符的重载

private:
 T x_offset;
 T y_offset;
 T z_offset;
};

template<class T>
void CVector3D<T>::VectorPrint()
{
 cout<<"("<<x_offset<<","<<y_offset<<","<<z_offset<<")"<<endl;
}
template<class T>
CVector3D<T>::CVector3D()
{
 x_offset = 0;
 y_offset = 0;
 z_offset = 0;
}

template<class T>
void CVector3D<T>::operator*()
{
 cout<<"("<<x_offset<<","<<y_offset<<","<<z_offset<<")"<<endl;
}

//template<class T>
//CVector3D<T>& CVector3D<T>::operator*()
//{
// return *this; 
//}

template<class T>
CVector3D<T>& CVector3D<T>::operator-=( const CVector3D &ob1 )
{
 x_offset -= ob1.x_offset;
 y_offset -= ob1.y_offset;
 z_offset -= ob1.z_offset;
 return *this;
}
template<class T>
CVector3D<T>& CVector3D<T>::operator+=( const CVector3D &ob1 )
{
 x_offset += ob1.x_offset;
 y_offset += ob1.y_offset;
 z_offset += ob1.z_offset;
 return *this;
}

template<class T>
bool CVector3D<T>::operator==( const CVector3D& obj )
{
 if(x_offset==obj.x_offset && y_offset==obj.y_offset && z_offset==obj.z_offset)
  return true;
 else
  return false;
}

template<class T>
CVector3D<T>& CVector3D<T>::operator=( const CVector3D& src )
{
 if(this==&src)
  return *this;
 x_offset = src.x_offset;
 y_offset = src.y_offset;
 z_offset = src.z_offset;
 return *this;
}
template<class T>
CVector3D<T> CVector3D<T>::operator-( const CVector3D &ob1 )
{
 CVector3D temp;
 temp.x_offset = x_offset - ob1.x_offset;
 temp.y_offset = y_offset - ob1.y_offset;
 temp.z_offset = z_offset - ob1.z_offset;
 return temp;
}
template<class T>
CVector3D<T> CVector3D<T>::operator+( const CVector3D &ob1 )
{
 CVector3D temp;
 temp.x_offset = x_offset + ob1.x_offset;
 temp.y_offset = y_offset + ob1.y_offset;
 temp.z_offset = z_offset + ob1.z_offset;
 return temp;
}

template<class T>
CVector3D<T> CVector3D<T>::Normalized()
{
 double moudle = this->VectorModulo();
 CVector3D temp;
 temp.x_offset = x_offset/moudle;
 temp.y_offset = y_offset/moudle;
 temp.z_offset = z_offset/moudle;
 return temp;
}

template<class T>
CVector3D<T> CVector3D<T>::CrossProduct( CVector3D<T>& aVector )
{
 CVector3D temp;
 temp.x_offset = y_offset*aVector.z_offset - z_offset*aVector.y_offset; //y1z2-z1y2
 temp.y_offset = z_offset*aVector.x_offset - x_offset*aVector.z_offset; //z1x2-x1z2
 temp.z_offset = x_offset*aVector.y_offset - y_offset*aVector.x_offset; //x1y2-y1x2
 return temp;
}

template<class T>
double CVector3D<T>::VectorAngle( CVector3D& aVector )
{
 return (x_offset*aVector.x_offset + y_offset*aVector.y_offset + z_offset*aVector.z_offset)
  /this->VectorModulo()*aVector.VectorModulo();
}

template<class T>
double CVector3D<T>::VectorModulo()
{
 //要加上long double
 return sqrt(long double(x_offset*x_offset+y_offset*y_offset+z_offset*z_offset));
}
template<class T>
double CVector3D<T>::DotProduct( CVector3D& aVector )
{
 return x_offset*aVector.x_offset + y_offset*aVector.y_offset + z_offset*aVector.z_offset;


}
template<class T>
CVector3D<T>::CVector3D( CVector3D &aVector )
{
 this->x_offset = aVector.x_offset;
 this->y_offset = aVector.y_offset;
 this->z_offset = aVector.z_offset;
}

 

template<class T>
CVector3D<T>::CVector3D( T x,T y,T z )
:x_offset(x)
,y_offset(y)
,z_offset(z)
{

}

 


int main()
{
 CVector3D<int> a(5,6,7),b(8,9,10);
 CVector3D<int> c;     //无参构造
 *c;                   //指针运算符'*'重载调用
 c = a+b;              // '+'
 *c;
 c += b;               // '+='
 *c;
 cout<<c.DotProduct(b)<<endl;
 cout<<c.VectorModulo()<<endl;
 CVector3D<int> d = c.CrossProduct(a);
 *d;
 CVector3D<int> e = d;  // '='
 *e;
 return 1;
}

 

阅读更多
文章标签: c
个人分类: 练习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭