类与对象2

重新定义people类。将所有成员设置为private私有访问权限,并在类中添加public公有访问权限的函数成员;生长(growth),使生长函数调用私有的函数成员进行进食、运动、睡眠。

#include<iostream>
using namespace std;
class CPeople{
private:
	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;
	};
public:
	void growth()//生长函数
	{
	cout<<"请输入年龄:";
	cin>>age;
	cout<<"请输入身高(单位:cm):";
	cin>>hight;
	cout<<"请输入体重(单位:kg):";
	cin>>weight;
	cout<<"调用前的结果:"<<endl;
	cout<<age<<" "<<hight<<" "<<weight<<endl;
    cout<<"调用进食函数后的结果:"<<endl;
		eatting();
		cout<<age<<" "<<hight<<" "<<weight<<endl;
		sporting();
		cout<<"再调用运动函数后的结果:"<<endl;
		cout<<age<<" "<<hight<<" "<<weight<<endl;
		sleeping();
		cout<<"最后调用睡眠函数后的结果:"<<endl;
		cout<<age<<" "<<hight<<" "<<weight<<endl;
	};
};
int main()
{
	CPeople p1;
	p1.growth();//调用生长函数
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值