用c++设计一个高校人员管理程序。首先设计一个基类Person(10分),包含编号、姓名、性别数据成员以及相关的成员函数,由它派生出教师类(10分)包括编号、姓名、性别、职称和工作学院数据的输入输出;再由Person类派生出Student类(10分),Student类派生出本科生类和研究生学生类,其中本科生类(10分)包括编号、姓名、性别、班号、英语、高等数学和数据结构三门课程成绩的输入输出并计算平均分;研究生类(10分)包括编号编号、姓名、性别、研究方向、导师数据的输入输出
时间: 2023-06-15 11:07:29 浏览: 105
以下是一个可能的C++代码实现:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 基类Person
class Person {
public:
Person(int id, string name, char gender) : id(id), name(name), gender(gender) {}
virtual void displayInfo() const {
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
}
protected:
int id;
string name;
char gender;
};
// 教师类
class Teacher : public Person {
public:
Teacher(int id, string name, char gender, string title, string college)
: Person(id, name, gender), title(title), college(college) {}
virtual void displayInfo() const {
Person::displayInfo();
cout << "职称:" << title << endl;
cout << "学院:" << college << endl;
}
private:
string title;
string college;
};
// 学生类
class Student : public Person {
public:
Student(int id, string name, char gender, string cls) : Person(id, name, gender), cls(cls) {}
virtual void displayInfo() const {
Person::displayInfo();
cout << "班号:" << cls << endl;
}
protected:
string cls;
};
// 本科生类
class Undergraduate : public Student {
public:
Undergraduate(int id, string name, char gender, string cls, double english, double math, double ds)
: Student(id, name, gender, cls), english(english), math(math), ds(ds) {}
virtual void displayInfo() const {
Student::displayInfo();
cout << "英语成绩:" << english << endl;
cout << "高等数学成绩:" << math << endl;
cout << "数据结构成绩:" << ds << endl;
cout << "平均分:" << calcAvg() << endl;
}
private:
double english;
double math;
double ds;
double calcAvg() const {
return (english + math + ds) / 3;
}
};
// 研究生类
class Graduate : public Student {
public:
Graduate(int id, string name, char gender, string cls, string research, string advisor)
: Student(id, name, gender, cls), research(research), advisor(advisor) {}
virtual void displayInfo() const {
Student::displayInfo();
cout << "研究方向:" << research << endl;
cout << "导师:" << advisor << endl;
}
private:
string research;
string advisor;
};
int main() {
vector<Person*> people;
people.push_back(new Teacher(1001, "张三", 'M', "教授", "计算机学院"));
people.push_back(new Undergraduate(2001, "李四", 'F', "计科1班", 80, 90, 85));
people.push_back(new Graduate(3001, "王五", 'M', "软件1班", "人工智能", "赵六"));
for (auto p : people) {
p->displayInfo();
cout << endl;
}
for (auto p : people) {
delete p;
}
return 0;
}
```
注意,本代码并没有包含输入数据的部分,只是展示了一个类的继承关系,并实现了派生类的部分成员函数。具体的输入和输出格式可以根据具体需求进行设计和实现。
阅读全文