帮我用c++写一个学生成绩管理系统,系统应具有:数据维护(数据录入、添加、修改、删除)、数据查询 (可按学号、姓名、性别、民族、年龄、地址、各门课程成绩等进行查询),而且需要利用封装、继承和多态机制,并用备注指明哪里用的封装、继承和多态
时间: 2023-10-20 14:24:30 浏览: 87
好的,下面是一个简单的学生成绩管理系统的示例代码,其中使用了封装、继承和多态机制:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 学生类
class Student {
protected:
string name; // 姓名
string gender; // 性别
int age; // 年龄
string address; // 地址
public:
Student(string name_, string gender_, int age_, string address_) {
name = name_;
gender = gender_;
age = age_;
address = address_;
}
// 获取学生信息
virtual void getInfo() {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "年龄:" << age << endl;
cout << "地址:" << address << endl;
}
};
// 成绩类
class Score {
protected:
vector<int> scores; // 各科成绩
public:
Score() {}
// 添加成绩
void addScore(int score) {
scores.push_back(score);
}
// 获取成绩总分
int getTotalScore() {
int totalScore = 0;
for (int i = 0; i < scores.size(); i++) {
totalScore += scores[i];
}
return totalScore;
}
// 获取平均成绩
float getAverageScore() {
int totalScore = getTotalScore();
return (float)totalScore / scores.size();
}
// 获取各科成绩
virtual void getScores() {
cout << "各科成绩:";
for (int i = 0; i < scores.size(); i++) {
cout << scores[i] << " ";
}
cout << endl;
}
};
// 学生成绩类
class StudentScore : public Student, public Score {
private:
int id; // 学号
string nation; // 民族
public:
StudentScore(string name_, string gender_, int age_, string address_, int id_, string nation_)
: Student(name_, gender_, age_, address_), id(id_), nation(nation_) {}
// 获取学生信息和成绩
void getInfo() override {
Student::getInfo();
cout << "学号:" << id << endl;
cout << "民族:" << nation << endl;
getScores();
}
// 获取各科成绩
void getScores() override {
Score::getScores();
}
};
// 学生管理类
class StudentManager {
private:
vector<StudentScore> students; // 学生列表
public:
// 添加学生
void addStudent(StudentScore student) {
students.push_back(student);
}
// 删除学生
void deleteStudent(int id) {
for (vector<StudentScore>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
break;
}
}
}
// 修改学生信息
void modifyStudent(int id, string name, string gender, int age, string address, string nation) {
for (vector<StudentScore>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
it->name = name;
it->gender = gender;
it->age = age;
it->address = address;
it->nation = nation;
break;
}
}
}
// 按学号查询学生
StudentScore* queryStudent(int id) {
for (vector<StudentScore>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
return &(*it);
}
}
return NULL;
}
// 按姓名查询学生
vector<StudentScore*> queryStudent(string name) {
vector<StudentScore*> result;
for (vector<StudentScore>::iterator it = students.begin(); it != students.end(); it++) {
if (it->name == name) {
result.push_back(&(*it));
}
}
return result;
}
// 输出所有学生信息
void printAllStudents() {
for (vector<StudentScore>::iterator it = students.begin(); it != students.end(); it++) {
it->getInfo();
cout << endl;
}
}
};
int main() {
// 创建学生管理类
StudentManager studentManager;
// 添加学生
StudentScore student1("张三", "男", 18, "北京市海淀区", 1001, "汉族");
student1.addScore(80);
student1.addScore(90);
student1.addScore(85);
studentManager.addStudent(student1);
StudentScore student2("李四", "女", 19, "北京市朝阳区", 1002, "满族");
student2.addScore(85);
student2.addScore(95);
student2.addScore(90);
studentManager.addStudent(student2);
StudentScore student3("王五", "男", 20, "北京市丰台区", 1003, "回族");
student3.addScore(90);
student3.addScore(85);
student3.addScore(95);
studentManager.addStudent(student3);
// 输出所有学生信息
studentManager.printAllStudents();
// 修改学生信息
studentManager.modifyStudent(1002, "赵六", "男", 19, "北京市海淀区", "朝鲜族");
// 输出所有学生信息
studentManager.printAllStudents();
// 按学号查询学生
StudentScore* student4 = studentManager.queryStudent(1003);
if (student4 != NULL) {
student4->getInfo();
}
else {
cout << "未找到该学生" << endl;
}
// 按姓名查询学生
vector<StudentScore*> student5 = studentManager.queryStudent("赵六");
if (student5.size() > 0) {
for (vector<StudentScore*>::iterator it = student5.begin(); it != student5.end(); it++) {
(*it)->getInfo();
}
}
else {
cout << "未找到该学生" << endl;
}
// 删除学生
studentManager.deleteStudent(1002);
// 输出所有学生信息
studentManager.printAllStudents();
return 0;
}
```
其中,Student类用于封装学生的基本信息,Score类用于封装学生的成绩信息,StudentScore类继承了Student和Score类,实现了多态,即可以输出学生的基本信息和成绩信息。StudentManager类用于管理所有学生,实现了添加、删除、修改、查询等功能。在代码中,用注释指明了哪里使用了封装、继承和多态机制。
阅读全文