设计一个People 类,该类的数据成员有姓名、年龄、身高、体重和人数,其中人数为静态数据成员,成员函数有构造函数、显示和显示人数。其中构造函数由参数姓名、年龄、身高和体重来构造对象;显示函数用于显示人的姓名、年龄、身高和体重;显示人数函数为静态成员函数,用于显示总的人数。
输入格式:
按姓名、年龄、身高和体重依次输入每个人的信息 已exit结束 zhang 18 180 70 li 20 160 50 exit
输出格式:
输出总人数,例如 2
输入样例:
在这里给出一组输入。例如:
zhang 18 180 70
li 20 160 50
exit
结尾无空行
输出样例:
在这里给出相应的输出。例如:
2
结尾无空行
代码:
# include <iostream>
using namespace std;
class PeoPle {
static int cnt;
private:
string name ;
int age ;
int height ;
int weight ;
public:
PeoPle()
{
name = "名字未输入" ;
age = 0 ;
height = 0 ;
weight = 0 ;
}
void input()
{
cin >> name;
while ( name != "exit")
{
cin >> age >> height >> weight ;
cnt ++ ;
cin >> name;
}
}
static void output()
{
cout << cnt << endl;
}
};
int PeoPle::cnt;
int main()
{
PeoPle ob;
ob.input();
ob.output();
return 0;
}