用c++写一个学校师生管理系统
时间: 2023-08-04 11:10:22 浏览: 84
好的,以下是一个简单的学校师生管理系统的代码示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Student {
public:
string name;
int age;
string gender;
string id;
Student(string n, int a, string g, string i) {
name = n;
age = a;
gender = g;
id = i;
}
};
class Teacher {
public:
string name;
int age;
string gender;
string id;
Teacher(string n, int a, string g, string i) {
name = n;
age = a;
gender = g;
id = i;
}
};
class School {
public:
vector<Student> students;
vector<Teacher> teachers;
void addStudent(Student s) {
students.push_back(s);
}
void addTeacher(Teacher t) {
teachers.push_back(t);
}
void printStudents() {
for (int i = 0; i < students.size(); i++) {
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "Gender: " << students[i].gender << endl;
cout << "ID: " << students[i].id << endl;
cout << endl;
}
}
void printTeachers() {
for (int i = 0; i < teachers.size(); i++) {
cout << "Name: " << teachers[i].name << endl;
cout << "Age: " << teachers[i].age << endl;
cout << "Gender: " << teachers[i].gender << endl;
cout << "ID: " << teachers[i].id << endl;
cout << endl;
}
}
};
int main() {
School school;
Student s1("Bob", 18, "Male", "1001");
Student s2("Alice", 17, "Female", "1002");
Teacher t1("Mr. Smith", 35, "Male", "2001");
Teacher t2("Ms. Johnson", 28, "Female", "2002");
school.addStudent(s1);
school.addStudent(s2);
school.addTeacher(t1);
school.addTeacher(t2);
cout << "Students:" << endl;
school.printStudents();
cout << "Teachers:" << endl;
school.printTeachers();
return 0;
}
```
这个示例程序实现了一个简单的学校师生管理系统,其中包括了学生和教师两个类,以及学校类。在主函数中,我们创建了两个学生对象和两个教师对象,然后将它们添加到学校对象中。最后打印学生和教师的信息。
阅读全文