类与对象3

编写一个类定义形式的结构体变量people。该结构体变量包括数据成员有年龄(age)、身高(height)、体重(weight)、函数成员有进食(eating)、运动(sporting)、睡眠(sleeping)。在主函数中通过结构体变量访问类的所有成员。

#include<iostream>
using namespace std;
struct people
{
    int age;//年龄
	int hight;//身高
	int weight;//体重
	void eatting()//进食函数
	{weight=weight+1;};
	void sporting()//运动函数
	{hight=hight+1;};
	void sleeping()//睡眠函数
	{
		age=age+1;
		hight=hight+1;
		weight=weight+1;
	};
}p1;
int main()
{
	cout<<"请输入年龄:";
	cin>>p1.age;
	cout<<"请输入身高(单位:cm):";
	cin>>p1.hight;
	cout<<"请输入体重(单位:kg):";
	cin>>p1.weight;
	cout<<"调用前的结果:"<<endl;;
	cout<<p1.age<<" "<<p1.hight<<" "<<p1.weight<<endl;
	p1.eatting();
	cout<<"调用进食函数后的结果:"<<endl;
	cout<<p1.age<<" "<<p1.hight<<" "<<p1.weight<<endl;
	p1.sleeping();
	cout<<"调用睡眠函数后的结果:"<<endl;
	cout<<p1.age<<" "<<p1.hight<<" "<<p1.weight<<endl;
	p1.sporting();
	cout<<"调用运动函数后的结果:"<<endl;
	cout<<p1.age<<" "<<p1.hight<<" "<<p1.weight<<endl;
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值