一个教学系统至少有学生和教师两种类型的人员,假设教师的数据有教师编号、姓名、年龄、性别、职称和系别,学生的数据有学号、姓名、年龄、性别、班级和语文、数学、英语三门课程的成绩。编程完成学生和教师档案数据的输入和显示。要求如下: 设计三个类Person、Teacher、Student,Person是Teacher和Student的基类,具有此二类共有的数据成员姓名、年龄、性别,并具有输入和显示这些数据的成员函数;Teacher类继承了Person类的功能,并增加对教师编号、职称和系别等数据成员进行输入和显示的成员函数。按同样的方法完善Student类的设计。 对本题目进行以下扩展: (1) 在Person的派生类中增加学生(Student)类人员,教师分为普通教师(Teacher)、院长(Chancellor)和系主任(Dean)三类。所有教师都有基本工资(salary),普通教师的总收入income由基本工资和课时费构成。一个教师要管理若干学生,而一个学生只有一位教师作导师; (2) 院长和系主任都从普通教师中产生,院长的总收入由年薪和基本工资构成,系主任的总收入由基本工资和每有绩效构成。他们每年须完成指定课时的教学任务,但不计课时报酬。c++代码实现
时间: 2024-03-22 10:40:28 浏览: 54
用c++编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。
5星 · 资源好评率100%
以下是C++代码实现:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person {
public:
string name;
int age;
char gender;
virtual void input() {
cout << "Name: ";
cin >> name;
cout << "Age: ";
cin >> age;
cout << "Gender(M/F): ";
cin >> gender;
}
virtual void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
class Teacher : public Person {
public:
int teacher_id;
string title;
string department;
void input() {
Person::input();
cout << "Teacher ID: ";
cin >> teacher_id;
cout << "Title: ";
cin >> title;
cout << "Department: ";
cin >> department;
}
void display() {
Person::display();
cout << "Teacher ID: " << teacher_id << endl;
cout << "Title: " << title << endl;
cout << "Department: " << department << endl;
}
};
class Student : public Person {
public:
string student_id;
string grade;
double chinese_score;
double math_score;
double english_score;
void input() {
Person::input();
cout << "Student ID: ";
cin >> student_id;
cout << "Grade: ";
cin >> grade;
cout << "Chinese Score: ";
cin >> chinese_score;
cout << "Math Score: ";
cin >> math_score;
cout << "English Score: ";
cin >> english_score;
}
void display() {
Person::display();
cout << "Student ID: " << student_id << endl;
cout << "Grade: " << grade << endl;
cout << "Chinese Score: " << chinese_score << endl;
cout << "Math Score: " << math_score << endl;
cout << "English Score: " << english_score << endl;
}
};
class Chancellor : public Teacher {
public:
double annual_salary;
void input() {
Teacher::input();
cout << "Annual Salary: ";
cin >> annual_salary;
}
void display() {
Teacher::display();
cout << "Annual Salary: " << annual_salary << endl;
}
double get_income() {
return annual_salary + Teacher::salary;
}
};
class Dean : public Teacher {
public:
double performance;
void input() {
Teacher::input();
cout << "Performance: ";
cin >> performance;
}
void display() {
Teacher::display();
cout << "Performance: " << performance << endl;
}
double get_income() {
return Teacher::salary + performance;
}
};
class Management {
public:
vector<Student> students;
vector<Teacher*> teachers;
void add_student() {
Student student;
student.input();
students.push_back(student);
}
void add_teacher() {
Teacher* teacher;
int type;
cout << "Enter teacher type(1: Normal Teacher, 2: Chancellor, 3: Dean): ";
cin >> type;
switch(type) {
case 1:
teacher = new Teacher();
break;
case 2:
teacher = new Chancellor();
break;
case 3:
teacher = new Dean();
break;
default:
cout << "Invalid teacher type." << endl;
return;
}
teacher->input();
teachers.push_back(teacher);
}
void display_students() {
for (int i = 0; i < students.size(); i++) {
students[i].display();
cout << endl;
}
}
void display_teachers() {
for (int i = 0; i < teachers.size(); i++) {
teachers[i]->display();
cout << endl;
}
}
void assign_teacher() {
int teacher_id;
string student_id;
cout << "Enter teacher ID: ";
cin >> teacher_id;
for (int i = 0; i < teachers.size(); i++) {
if (teachers[i]->teacher_id == teacher_id) {
cout << "Enter student ID: ";
cin >> student_id;
for (int j = 0; j < students.size(); j++) {
if (students[j].student_id == student_id) {
students[j].teacher = teachers[i];
cout << "Teacher assigned." << endl;
return;
}
}
cout << "Student not found." << endl;
return;
}
}
cout << "Teacher not found." << endl;
}
void calculate_income() {
for (int i = 0; i < teachers.size(); i++) {
double income = teachers[i]->get_income();
cout << teachers[i]->name << "'s income is " << income << "." << endl;
}
}
};
int main() {
Management management;
int option;
do {
cout << "Enter option(1: Add Student, 2: Add Teacher, 3: Display Students, 4: Display Teachers, 5: Assign Teacher, 6: Calculate Income, 0: Quit): ";
cin >> option;
switch(option) {
case 1:
management.add_student();
break;
case 2:
management.add_teacher();
break;
case 3:
management.display_students();
break;
case 4:
management.display_teachers();
break;
case 5:
management.assign_teacher();
break;
case 6:
management.calculate_income();
break;
case 0:
cout << "Quitting program..." << endl;
break;
default:
cout << "Invalid option." << endl;
break;
}
} while(option != 0);
return 0;
}
```
阅读全文