5.编写设计一个People(人)类。该类的数据成员有年龄(age)、身高(height)、体重(weight)和人数(num),其中人数为静态数据成员,成员函数有构造函数(People)、进食(Eatting)、运动(Sporting)、睡眠(Sleeping)、显示(Show)和显示人数(ShowNum)。其中构造函数由已知参数年龄(a)、身高(h)和体重(w)构造对象,进食函数使体重加1,运动函数使身高加1,睡眠函数使年龄、身高、体重各加1,显示函数用于显示人的年龄、身高、体重,显示人数函数为静态成员函数,用于显示人的个数。假设年龄的单位为岁,身高的单位为厘米,体重的单位为市斤,要求所有数据成员为protected访问权限,所有成员函数为public访问权限,在主函数中通过对象直接访问类的所有成员函数。
using namespace std;
class People{
protected:
int age,height,weight;
static int num;
public:
People(int a,int b,int c):height(a),weight(b),age(c){num++;}
~People(){}
friend void Eating(People a);
friend void Sporting(People a);
friend void Sleeping(People a);
void Show();
void ShowNum();
int getweight(){return weight;}
int getheight(){return height; }
int getage(){return age;}
int getnum(){return num;}
};
int People::num=0;
void Eating(People a){
a.weight=a.weight+1;
}
void Sporting(People a){
a.height=a.height+1;
}
void Sleeping(People a){
a.weight=a.weight+1;
a.height=a.height+1;
a.age=a.age+1;
}
void People::Show(){
cout<<"weight:"<<getweight()<<"\nheight:"<<getheight()<<"\nage:"<<getage()<<endl;
}
void People::ShowNum(){
cout<<"num:"<<getnum()<<endl;
}
void main(){
People b(176,55,20);
Eating(b);
Sporting(b);
Sleeping(b);
b.Show();
b.ShowNum();
}