5.编写设计一个People(人)类。该类的数据成员有年龄(age)、身高(height)、体重(weight)和人数(num),其中人数为静态数据成员,成员函数有构造函数(People)、进食(Eatting)、运动(Sporting)、睡眠(Sleeping)、显示(Show)和显示人数(ShowNum)。其中构造函数由已知参数年龄(a)、身高(h)和体重(w)构造对象,进食函数使体重加1,运动函数使身高加1,睡眠函数使年龄、身高、体重各加1,显示函数用于显示人的年龄、身高、体重,显示人数函数为静态成员函数,用于显示人的个数。假设年龄的单位为岁,身高的单位为厘米,体重的单位为市斤,要求所有数据成员为protected访问权限,所有成员函数为public访问权限,在主函数中通过对象直接访问类的所有成员函数。
6.#include<iostream>
7.using namespace std;
8.class People
9.{
10.protected:
11. int age;
12. double weight,height;
13. static int num;
14.public:
15. People (int a,double w,double h):age(a),weight(w),height(h){num++;}
16. void Eatting(){weight++;}
17. void Sporting(){height++;}
18. void Sleeping(){age++;weight++;height++;}
19. void Show()
20. {cout<<"age weight height"<<endl;
21. cout<<age<<"(岁)"<<'\t'<<weight<<"(市斤)"<<'\t'<<height<<"(厘米)"<<endl;
22. }
23. static void ShowNum()
24. {cout<<num<<endl;}
25.};
26.int People::num=0;
27.
28.void main()
29.{
30. People a(20,50,180);
31. a.Eatting();
32. a.Sporting();
33. a.Sleeping();
34. a.Show();
35. a.ShowNum();
36.}