Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16174
  • 博文数量: 14
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-28 13:45
文章分类

全部博文(14)

文章存档

2013年(9)

2012年(5)

我的朋友

分类: 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
请按任意键继续. . .

阅读(233) | 评论(0) | 转发(0) |
0

上一篇:拷贝构造函数

下一篇:标准的交换SWAP

给主人留下些什么吧!~~