struct 重载 和 继承 的使用例子

本文提供了一个C++示例程序,展示了如何在结构体中实现运算符重载以及通过派生类继承基类的方式扩展结构体的功能。通过对点(CPoint)和矩形(CRect/CRect2)的定义与操作,具体说明了加法、乘法等运算符的重载方法及矩形类的继承关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值