学生信息管理系统c++
时间: 2023-10-18 09:12:25 浏览: 34
好的,学生信息管理系统是一个常见的C++练手项目,可以帮助你巩固C++的基础语法和面向对象编程的知识。下面是一个简单的学生信息管理系统的代码示例,供你参考:
```c++
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
class Student {
private:
string name;
int age;
string gender;
int score;
public:
Student(string name, int age, string gender, int score) {
this->name = name;
this->age = age;
this->gender = gender;
this->score = score;
}
string getName() {
return name;
}
int getAge() {
return age;
}
string getGender() {
return gender;
}
int getScore() {
return score;
}
void setName(string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setGender(string gender) {
this->gender = gender;
}
void setScore(int score) {
this->score = score;
}
void printInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Score: " << score << endl;
}
};
int main() {
vector<Student*> students;
int choice;
while (true) {
cout << "1. Add a student" << endl;
cout << "2. Print all students" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
string name, gender;
int age, score;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Enter gender: ";
cin >> gender;
cout << "Enter score: ";
cin >> score;
Student* student = new Student(name, age, gender, score);
students.push_back(student);
cout << "Student added successfully!" << endl;
} else if (choice == 2) {
if (students.size() == 0) {
cout << "No students added yet!" << endl;
} else {
for (int i = 0; i < students.size(); i++) {
students[i]->printInfo();
cout << endl;
}
}
} else if (choice == 3) {
break;
} else {
cout << "Invalid choice!" << endl;
}
}
return 0;
}
```
这是一个简单的命令行程序,通过数字选择相应的操作,可以添加学生信息、打印所有学生信息、退出程序。在这个示例中,我们使用了一个名为`Student`的类来表示学生,其中包含了学生的姓名、年龄、性别和分数等信息。我们使用了一个名为`vector`的STL容器来存储所有的学生对象,通过`push_back`方法将学生对象添加到容器中,通过循环遍历容器中的所有学生对象并调用`printInfo`方法来打印所有学生信息。这只是一个简单的示例,你可以根据自己的需求对其进行更改和扩展。