• 自己写的Point类


    今天自己写了个Point类,对最近的模板学习、运算符重载和复制构造函数进行了验证。以下是头文件:

     1 #ifndef _POINT_H_
    2 #define _POINT_H_
    3 #include <iostream>
    4 using namespace std;
    5 template <class DataType>
    6 class Point
    7 {
    8 public:
    9 Point();
    10 Point(DataType a, DataType b) : x(a), y(b) {}
    11 Point(const Point & pt);
    12 ~Point();
    13
    14 void SetX(DataType a);
    15 void SetY(DataType b);
    16 DataType GetX() const;
    17 DataType GetY() const;
    18
    19 void Show() const;
    20
    21 Point & operator = (const Point & right);
    22
    23 Point & operator + (const Point & right);
    24 Point & operator + (const DataType & right);
    25 Point & operator - (const Point & right);
    26 Point & operator - (const DataType & right);
    27 Point & operator * (const Point & right);
    28 Point & operator * (const DataType & right);
    29 Point & operator / (const Point & rihgt);
    30 Point & operator / (const DataType & right);
    31 Point & operator % (const Point & right);
    32 Point & operator % (const DataType & right);
    33
    34 Point * operator & (const Point & pt);
    35 Point & operator ++ ();
    36 Point & operator -- ();
    37
    38 operator double();
    39
    40 private:
    41 DataType x;
    42 DataType y;
    43 };
    44 #endif

    源文件:

    View Code
    #include "Point.h"

    template <class DataType>
    Point<DataType>::Point()
    {
    x = 0;
    y = 0;
    }

    template <class DataType>
    Point<DataType>::~Point()
    {}

    template <class DataType>
    Point<DataType>::Point(const Point & pt)
    {
    x = pt.x;
    y = pt.y;
    }


    template <class DataType>
    void Point<DataType>::SetX(DataType a)
    {
    x = a;
    }

    template <class DataType>
    void Point<DataType>::SetY(DataType b)
    {
    y = b;
    }

    template <class DataType>
    DataType Point<DataType>::GetX() const
    {
    return x;
    }

    template <class DataType>
    DataType Point<DataType>::GetY() const
    {
    return y;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator = (const Point<DataType> &right)
    {
    if(this == &right)
    return *this;
    x = right.x;
    y = right.y;

    return *this;
    }

    template <class DataType>
    void Point<DataType>::Show() const
    {
    cout<<"("<<x<<", "<<y<<")"<<endl;
    }

    template <class DataType>
    Point<DataType> * Point<DataType>::operator & (const Point<DataType> &pt)
    {
    return this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator + (const Point<DataType> & right)
    {
    x += right.x;
    y += right.y;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator + (const DataType & right)
    {
    x += right;
    y += right;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator - (const Point<DataType> & right)
    {
    x -= right.x;
    y -= right.y;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator - (const DataType & right)
    {
    x -= right;
    y -= right;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator * (const Point<DataType> & right)
    {
    x *= right.x;
    y *= right.y;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator * (const DataType & right)
    {
    x *= right;
    y *= right;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator / (const Point<DataType> & right)
    {
    x /= right.x;
    y /= right.y;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator / (const DataType & right)
    {
    x /= right;
    y /= right;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator % (const Point<DataType> & right)
    {
    x %= right.x;
    y %= right.y;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator % (const DataType & right)
    {
    x %= right;
    y %= right;

    return *this;
    }


    template <class DataType>
    Point<DataType> & Point<DataType>::operator ++ ()
    {
    x ++;
    y ++;

    return *this;
    }

    template <class DataType>
    Point<DataType> & Point<DataType>::operator -- ()
    {
    x --;
    y --;

    return *this;
    }

    template <class DataType>
    Point<DataType>::operator double()
    {
    x = (double)x;
    y = (double)y;

    return *this;
    }


     

  • 相关阅读:
    [java,2019-01-28] 枪手博弈,谁才是最后赢家
    [java,2019-01-25] 图片和二进制互转
    [java,2019-01-15] word转pdf
    [python,2018-06-29] 37%法则及其拓展解决恋爱问题
    [java,2018-06-26] 扑克牌抽牌求和问题
    [python,2018-06-25] 高德纳箭号表示法
    [java,2017-06-12] myEclipse双击无法打开文件
    OpenGL核心技术之法线贴图
    游戏中水的渲染技术系列一
    Unity 3D实现帧同步技术
  • 原文地址:https://www.cnblogs.com/lscheng/p/2220572.html
Copyright © 2020-2023  润新知