设计一个学生信息管理程序,管理10个学生信息,写一个西数能够按学号查询输出指定学生的信息 (姓名, ,课程,成绩)。 要求:1) 在主函数中采用対象数 组形式实例化10个学生对象;2) 输出函数采用非成员函数形式,以对象指针为形参;3) 采用标准vector动态数组存 储学生对象,并在类中增加静态数据成员count,实现对象 生成的汁数。
时间: 2024-03-26 12:36:31 浏览: 75
编程实现学生基本信息管理程序
下面是代码实现:
```c++
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Student {
public:
Student(string name, int id, string course, double score) {
this->name = name;
this->id = id;
this->course = course;
this->score = score;
count++;
}
string getName() {
return name;
}
int getId() {
return id;
}
string getCourse() {
return course;
}
double getScore() {
return score;
}
static int count;
private:
string name;
int id;
string course;
double score;
};
int Student::count = 0;
void printInfo(Student *stu) {
cout<<"姓名:"<<stu->getName()<<endl;
cout<<"学号:"<<stu->getId()<<endl;
cout<<"课程:"<<stu->getCourse()<<endl;
cout<<"成绩:"<<stu->getScore()<<endl;
}
int main() {
vector<Student> students;
students.push_back(Student("张三", 1001, "语文", 90));
students.push_back(Student("李四", 1002, "数学", 85));
students.push_back(Student("王五", 1003, "英语", 92));
students.push_back(Student("赵六", 1004, "物理", 88));
students.push_back(Student("孙七", 1005, "化学", 86));
students.push_back(Student("周八", 1006, "历史", 89));
students.push_back(Student("吴九", 1007, "地理", 91));
students.push_back(Student("郑十", 1008, "政治", 87));
students.push_back(Student("钱十一", 1009, "生物", 90));
students.push_back(Student("马十二", 1010, "体育", 93));
int id;
cout<<"请输入要查询的学生的学号:";
cin>>id;
for(Student &stu : students) {
if(stu.getId() == id) {
printInfo(&stu);
break;
}
}
return 0;
}
```
输出结果为:
```
请输入要查询的学生的学号:1005
姓名:孙七
学号:1005
课程:化学
成绩:86
```
阅读全文