处理学生成绩问题的程序.c

来自「解决实际问题:教师统计学生考试成绩以及超市、商场等统计货物情况。」· C语言 代码 · 共 56 行

C
56
字号
定义一个学生类Student,包括3个数据成员:学号id,姓名name,分数score;两个静态数据成员:总分total和学生人数count;带参数的析构函数用来初始化对象,成员函数ChangeScore用来修改分数,静态成员函数GetAverage用来打印计算得到的平均分。
#include <iostream.h>
class Student
{
public:
Student(char*x,char*y,,float z);
void ChangeScore(const float s);
static void GetAverage();
private:
char id[15];
char name[40];
float score;
static float total;
static int count;
};
float Student::total=0;                  //静态数据成员的定义
int Student::count=0;
Student::Student(char*x,char*y,,float z)
{
strcpy(id,x);
strcpy(name,y);
score=z;
count++;
total+=z;
}
void Student::ChangeScore(const float s);
{
total-=score;
score=s;
total+=s;
}
void Student::GetAverage()
{
cout<<"The average score of the "<<count<<"student is "<<total/count<<endl;
}
int main()
{
Student s1("20040201","wangling",90);
s1.GetAverage();                         //通过对象名访问
Student s2("20030324","Liming",87);
s1.ChangeScore(89);                         
 Student::GetAverage();                                       //通过类名访问
return 0












⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?