/*定义学生类,包含姓名char name[20],性别 char sex ,int score ;统计男女人数,平均成绩*/
#include<iostream>
#include <cstring>using namespace std;
class student
{
private:
char name[20];
char *name_p;
char sex;
int score;
static int j;
static int score_total;
public:
student(char *pn,char se,int sc)//可以用指针来实现
{
//static int i=0;
//++i;//仅计数有参数的对象
++j;//计数创建的所有对象
strcpy(name,pn);
sex=se;//性别进行赋值
score=sc;//成绩值的设置
score_total+=score;//统计所有对象的成绩和
}
char read_sex(){return sex;}
static int read_total(){return j;}
static int read_total_score(){return score_total;}
};
int student::j=0;
int student::score_total=0;
float aver_score();
void main()
{
student s1("lj",'m',100),s2("wan",'w',98),s3("wann",'m',97);
cout<<student::read_total()<<endl;
cout<<"总分="<<student::read_total_score()<<endl;
cout<<"平均分="<<aver_score()<<endl;
int peo=student::read_total();
int m_total=0;
student s[3]={s1,s2,s3};
for (int k=0;k<peo;k++)
if(s[k].read_sex()=='m')
++m_total;
cout<<"所有的男生人数:"<<m_total<<endl;
cout<<"所有的女生人数:"<<peo-m_total<<endl;
}float aver_score()
{
return student::read_total_score()/student::read_total();
}