#include "iostream" #include "string" using namespace std; static int num=0; //存放学生总数; static float sum=0.0; //存放学生成绩总分数; //定义学生类 class student { int no; //学号; string name; //姓名; float deg; //成绩; public: public: void setdata(student& s1); //学生数据成员赋值接口; void disp(student s2); //输出接口; static float avg(); }; //学生数据录入; void student::setdata(student& s1) { cout<<"输入学号:"<<endl; cin>>s1.no; cout<<"输入学生姓名:"<<endl; cin>>s1.name; cout<<"输入学生成绩:"<<endl; cin>>s1.deg; sum+=s1.deg; num++; } //学生数据输出; void student::disp(student s2) { cout<<"学生的信息如下:"<<endl; cout<<s2.no<<endl; cout<<s2.name<<endl; cout<<s2.deg<<endl; } //计算平均分; float student::avg() { cout<<"学生成绩的平均分为:"; return (sum/num); } void main() { const int m=2; student stu[m]; for (int i=0;i<m;i++) { stu[i].setdata(stu[i]); } for (int j=0;j<m;j++) { stu[j].disp(stu[j]); } cout<<student::avg(); }