C++ 结构体

1、结构体定义

用结构体定于如下图位置

#include<iostream>
using  namespace std;
struct RECT {
	int top{ 1 };
	int bottom{ 1 };
	int left{ 1 };
	int right{ 1 };
};
 void  MoveRECT (RECT &aRect,int x, int y) //直接引用实参进行位置移动
 {
	 aRect.top = aRect.top + y;
	 aRect.bottom = aRect.bottom + y;
	aRect.left= aRect.left+x;
	aRect.right = aRect.right + x;
};

int main()
{

	RECT yard{ 0,0,100,120 };
	RECT pool{ 30,40,70,80 };
	RECT hut1, hut2;
	hut1.top = 10;
	hut1.bottom = 30;
	hut1.left = 70;
	hut1.right = hut1.left + 25;
	hut2 = hut1;
	MoveRECT(hut2, -60, 80); 
	cout << hut2.left;
	return 0;
}

2. 指针访问结构体成员

包括直接访问和间接方案

#include<iostream>
using  namespace std;
struct RECT {
	int top{ 1 };
	int bottom{ 1 };
};

int main()
{

	RECT aRect{ 3, 4 }; //结构体对象初始化
	RECT* PaRect;
	PaRect = &aRect;
	cout << (*PaRect).top; //访问结构体成员
	cout << PaRect->bottom;//间访结构体成员
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值