c加加学生管理系统代码
时间: 2024-06-11 21:01:42 浏览: 132
C++学生管理系统是一个常见的编程习项目,用于管理学生信息的录入、查询、修改和删除等。以下是一个简单的C++学生管理系统代码示例:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
private:
string name;
int age;
string id;
public:
Student(string name, int age, string id) {
this->name = name;
this->age = age;
this->id = id;
}
string getName() {
return name;
}
int getAge() {
return age;
}
string getId() {
return id;
}
};
// 学生管理系统类
classManagementSystem {
private:
vector<Student> students;
public:
void addStudent(Student student) {
students.push_back(student);
}
void displayAllStudents() {
for (int i = 0; i < students.size(); i++) {
cout << "Name: " << students[i].getName() << endl;
cout << "Age: " << students[i].getAge() << endl;
cout << "ID: " << students[i].getId() << endl;
cout << endl;
}
}
void searchStudent(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
cout << "Name: " << students[i].getName() << endl;
cout << "Age: " << students[i].getAge() << endl;
cout << "ID: " << students[i].getId() << endl;
return;
}
}
cout << "Student not found." << endl;
}
void deleteStudent(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
students.erase(students.begin() + i);
cout << "Student deleted." << endl;
return;
}
}
cout << "Student not found." << endl;
}
};
int main() {
StudentManagementSystem system;
// 添加学生
Student student1("Alice", 18, "1001");
Student student2("Bob", 19, "1002");
system.addStudent(student1);
system.addStudent(student2);
// 显示所有学生
system.displayAllStudents();
// 查询学生
system.searchStudent("1001");
// 删除学生
system.deleteStudent("1002");
return 0;
}
```
这段代码实现了一个简单的学生管理系统,包括添加学生、显示所有学生、查询学生和删除学生等功能。你可以根据自己的需求进行扩展和修改。
阅读全文