C++运算符的重载

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class point
{
      int x;
      int y;
      public :
             point point::operator+(const point &a);
             point()
             {
             }
             point(int x,int y)
             {
                           this->x = x;
                           this->y = y;
             }
             void toString()
             {
                  cout<<"x = "<<this->x<<"\ty = "<<this->y<<endl;
             }
};
point point::operator+(const point &a)
{
      point temp_point;
      temp_point.x = this->x + a.x;
      temp_point.y = this->y + a.y;
      return temp_point;
}
int main()
{
    point p1(2,3);
    point p2(4,5);
    p1.toString();
    p2.toString();
    point p3 = p1 + p2;
    p3.toString();
    system("pause");
    return 0;
}

本文遵守 CC-BY-NC-4.0 许可协议。

Creative Commons License

欢迎转载,转载需注明出处,且禁止用于商业目的。

上篇扫雷当点到空白处的迭代算法
下篇关于malloc的一些说明