该类的数据成员有Name,Age,Height,Weight,成员函数有构造函数People,进食Eating,运动Sporting,显示Show, 其中构造函数用已知参数姓名nm、年龄a、身高h、体重w构造对象,进食函数使体重加1,运动函数使身高加1,显示函数用于显示姓名、年龄、身高、体重。 要求数据成员都是private,成员函数都是public访问权限。
#include <string>
#include<iostream>
using namespace std;
class People{
private:
string name;
int age;
int height;
int weight;
public:
People(string nm,int a,int b,int c)
{
name=nm;
age=a;
height=b;
weight=c;
}
void Eating(){
weight++;
}
void Sporting(){
height++;
}
void Show();
};
void People::Show(){
cout<<"姓名 "<<name<<endl<<"年龄 "<<age<<endl<<"身高 "<<height<<endl<<"体重 "<<weight<<endl;
}
int main(){
string nm;
int a,b,c;
cin>>nm>>a>>b>>c;
People p(nm,a,b,c);
int i,x,y;
cin>>x>>y;
for(i=1;i<=x;i++)
{
p.Eating();
}
for(i=1;i<=y;i++)
{
p.Sporting();
}
p.Show();
}