📄 处理学生成绩问题的程序.c
字号:
定义一个学生类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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -