#include #include /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; /* 学生的基本信息 */ struct st_info{ int id; char name[30]; double grade; }; /* 创建一个学生类 */ class Student{ public: Student(st_info *st, int n=0){ count = n; sum = 0.0; // 开辟一段新的内存空间 this->st = new st_info[n]; /* 拷贝学生信息到新开辟的内存空间 */ for(int i=0; ist[i]), &st[i], sizeof(st_info)); // 这是C标准库的拷贝函数 包含头文件是C++ string类库 sum += st[i].grade; } } ~Student(){ deletet[] st; } /* 显示班级学生的信息 */ void show() const{ for(int i=0; i<count; i++){ cout << "id:"<< st[i].id << " name:" << st[i].name << " grade:" << st[i].grade << endl; } } /* 显示学生的平均分 */ void show_avg()const{ cout << "avg:" << sum/count << endl; } private: st_info *st; // 一个学生的信息 int count; // 学生的个数 int sum; // 总分数 }; int main(int argc, char** argv){ st_info st[3]; for(int i=0; i<3; i++){ cout < "Enter the student's id: "; cin >> st[i].id; cout < "Enter the student's name: "; cin >> st[i].name; cout < "Enter the student's grade: "; cin >> st[i].grade; } Student s(st,3); s.show(); s.show_avg(); return 0; }
追答下课的时候就玩会,应该早知道的 TptRw/www.767176f.com?JycNg