struct 重载 和 继承 的使用例子
#include "StdAfx.h"
#include <iostream>
#include <vector>
using namespace std;
typedef struct _CPoint
{
int x;
int y;
_CPoint ();
_CPoint (int a,int b);
_CPoint operator + (_CPoint point);
_CPoint operator * (_CPoint point);
_CPoint operator * (int point);
friend _CPoint operator - (_CPoint point1, _CPoint point2);
friend _CPoint operator * (int sum, _CPoint point);
friend bool operator > (_CPoint point1, _CPoint point2);
friend bool operator == (_CPoint point1, _CPoint point2);
}CPoint;
typedef struct _CRect
{
CPoint p1;
CPoint p2;
_CRect();
~_CRect();
_CRect(CPoint p1, CPoint p2);
}CRect;
typedef struct _CRect2 : _CRect
{
int width;
int height;
int num;
_CRect2();
~_CRect2();
_CRect2(CPoint p1, CPoint p2,int num);
void showMessage();
}CRect2;
bool operator == (_CPoint point1, _CPoint point2)
{
if ((point2.x == point1.x) && (point2.y == point1.y))
{
return true;
}
return false;
}
bool operator > (_CPoint point1, _CPoint point2)
{
if (point1.x > point2.x)
{
return true;
}
if ((point1.x == point2.x) && (point1.y > point2.y))
{
return true;
}
return false;
}
_CPoint operator * (int num, _CPoint point)
{
_CPoint sum;
sum.x = num * point.x;
sum.y = num * point.y;
return sum;
}
_CPoint operator - (_CPoint point1, _CPoint point2)
{
_CPoint sum;
sum.x = point1.x - point2.x;
sum.y = point1.y - point2.y;
return sum;
}
_CPoint :: _CPoint()
{
}
_CPoint :: _CPoint(int a,int b)
{
this->x=a;
this->y=b;
}
_CPoint _CPoint :: operator + (_CPoint point)
{
_CPoint sum;
sum.x = this->x + point.x;
sum.y = this->y + point.y;
return sum;
}
_CPoint _CPoint :: operator * (_CPoint point)
{
_CPoint sum;
sum.x = this->x * point.x;
sum.y = this->y * point.y;
return sum;
}
_CPoint _CPoint :: operator * (int num)
{
_CPoint sum;
sum.x = this->x * num;
sum.y = this->y * num;
return sum;
}
_CRect :: _CRect()
{
printf("\n _CRect()");
}
_CRect :: ~_CRect()
{
printf("\n ~_CRect()");
}
_CRect :: _CRect(CPoint p1, CPoint p2)
{
printf("\n _CRect(CPoint p1, CPoint p2)");
this->p1 = p1;
this->p2 = p2;
}
_CRect2 :: _CRect2()
{
printf("\n _CRect2()");
}
_CRect2 :: ~_CRect2()
{
printf("\n ~_CRect2()");
this->width = 0;
this->height = 0;
}
_CRect2 :: _CRect2(CPoint p1, CPoint p2, int num) : _CRect( p1, p2)
{
printf("\n _CRect2(CPoint p1, CPoint p2)");
this->num = num;
this->width = fabsf(this->p1.x - this->p2.x);
this->height = fabsf(this->p1.y - this->p2.y);
}
void _CRect2 :: showMessage()
{
printf("\n *****************************");
printf("\n -- num ---- %d", this->num);
printf("\n -- width -- %d", this->width);
printf("\n -- height -- %d", this->height);
printf("\n -- p1 ------ (%d, %d)", this->p1.x, this->p1.y);
printf("\n -- p2 ------ (%d, %d)", this->p2.x, this->p2.y);
printf("\n *****************************");
}
void xigou(CRect2 rect)
{
//CRect2 rect;
rect.showMessage();
}
void main(void)
{
CPoint p1(2,6);
CPoint p2(1,4);
CPoint p3;
p3 = p1 + p2;
p3 = p1 * p2;
p3 = p3 * 2;
p3 = p2 - p1;
p3 = p3 * 4;
bool result = false;
result = p1 > p2;
//CRect rect = CRect(p1,p2);
CRect2 rect;
xigou(rect);
CRect2 rect2 = CRect2(p1,p2,6);
rect2.showMessage();
return;
}