用c++写一个学籍管理系统
时间: 2024-05-07 20:21:45 浏览: 9
好的,以下是一个简单的学籍管理系统的 C++ 代码示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 学生类
class Student {
public:
Student(string n, string i, int a) : name(n), id(i), age(a) {}
string getName() const { return name; }
string getID() const { return id; }
int getAge() const { return age; }
void setName(string n) { name = n; }
void setID(string i) { id = i; }
void setAge(int a) { age = a; }
private:
string name;
string id;
int age;
};
// 学籍管理系统类
class StudentManagementSystem {
public:
void addStudent(const Student& s) { students.push_back(s); }
void deleteStudent(string id) {
for (auto it = students.begin(); it != students.end(); it++) {
if (it->getID() == id) {
students.erase(it);
break;
}
}
}
void printAllStudents() const {
cout << "所有学生信息:" << endl;
for (const auto& s : students) {
cout << "姓名:" << s.getName() << ",学号:" << s.getID() << ",年龄:" << s.getAge() << endl;
}
cout << endl;
}
private:
vector<Student> students;
};
int main() {
StudentManagementSystem sms;
// 添加学生
sms.addStudent(Student("张三", "10001", 18));
sms.addStudent(Student("李四", "10002", 19));
sms.addStudent(Student("王五", "10003", 20));
sms.addStudent(Student("赵六", "10004", 21));
sms.printAllStudents();
// 删除学生
sms.deleteStudent("10003");
sms.printAllStudents();
// 修改学生信息
for (auto& s : sms.getStudents()) {
if (s.getID() == "10002") {
s.setName("Tom");
s.setAge(20);
break;
}
}
sms.printAllStudents();
return 0;
}
```
该学籍管理系统使用了 C++ 中的类和 STL 容器(vector),实现了添加、删除和修改学生信息等功能。在实际开发中,还可以考虑增加文件读写等功能来保存学生信息。