分类: C/C++
2012-12-31 16:59:07
// operatorReload.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
class CPoint
{
public:
CPoint(int x1,int y1);
CPoint operator+(CPoint pt);
CPoint operator-(CPoint pt);
void operator=(CPoint pt);
void PrintPt();
private:
int x;
int y;
};
CPoint::CPoint(int x1,int y1)
{
this->x = x1;
this->y = y1;
}
CPoint CPoint::operator+(CPoint pt)
{
return CPoint(this->x+pt.x ,this->y+pt.y);
}
CPoint CPoint::operator-(CPoint pt)
{
return CPoint(this->x-pt.x, this->y-pt.y);
}
void CPoint::operator=(CPoint pt)
{
this->x = pt.x ;
this->y = pt.y ;
}
void CPoint::PrintPt()
{
printf("x = %d,y = %d \n",this->x,this->y);
}
int _tmain(int argc, _TCHAR* argv[])
{
CPoint pt1(200,100);
CPoint pt2(50,25);
CPoint pt3(0,0);
pt3.PrintPt();
pt3 = pt1;
pt3.PrintPt();
pt3 = pt1+pt2;
pt3.PrintPt();
pt3 = pt1-pt2;
pt3.PrintPt();
return 0;
}
执行后的结果为:
x = 0,y = 0
x = 200,y = 100
x = 250,y = 125
x = 150,y = 75
请按任意键继续. . .