题目:
- 编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
#include<iostream>
#include<string>
using namespace std;
class student
{
private:
string number; //学号
string name; //名字
double score; //分数
static int count; //人数
static double sum; //成绩
public:
student(string a,string b,float c)
{
number=a;
name=b;
score=c;
++count;
sum=sum+score;
}
void out1()
{
cout<<"学号:"<<number<<endl; //
cout<<"名字:"<<name<<endl; //
cout<<"分数:"<<score<<endl; //
}
void out2()
{
cout<<"学生人数为:"<<count<<endl;
cout<<"总成绩为:"<<sum<<endl;
cout<<"---------------"<<endl;
}
};
int student::count=0;
double student::sum=0.0;
int main()
{
student P1("0122","春",89.5);
P1.out1();
P1.out2();
student P2("0222","夏",68.5);
P2.out1();
P2.out2();
student P3("0322","秋",91.5);
P3.out1();
P3.out2();
return 0;
}