文章

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 4.0 进行授权