- 建立一个Student类实现如下功能:查找考试成绩不及格的学生及其学号,并统计这些学生的总人数。
- 类的定义要求如下:
私有数据成员
- Int num ——存储学生的编号,学生编号最小为1
- String name——存储学生姓名
- Float score——存储学生考试成绩
- Int count——统计符合条件的总人数
- 共有函数成员
- Void input()——输入学生成绩
- Void search()——查找不及格的学生人数并存入count变量
- Void print()——输出不及格总人数和不及格学生的编号、姓名、成绩
#include<iostream> using namespace std; const int SIZE = 3; //录入信息个数 class student{ private: int num; string name; float score; static int count; public: void input(); static void search(student stu[]); static void print(); }; int student::count = 0; //记录不及格学生人数 student *ptr[SIZE]; //外部变量,储存不及格 void student::input(){ cout<<"please input num:"<<endl; cin>>this->num; cout<<"please input name:"<<endl; cin>>this->name; cout<<"please input score:"<<endl; cin>>this->score; } void student::search(student stu[]){ int j=0; for(int i=0;i<SIZE;i++) { if(stu[i].score<60) { count++; ptr[j] = &stu[i]; j++; } } } void student::print(){ cout<<"不及格人数"<<count<<endl; for (int i = 0; i < count; i++) { cout << "编号:" << ptr[i]->num << " 成绩:" << ptr[i]->score << " 名字:" << ptr[i]->name << endl; } } int main(){ student stu[SIZE]; int i; for(i=0;i<SIZE;i++) { stu[i].input(); } student::search(stu); student::print(); return 0; }