定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类。教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。 编程要求: 1、设计虚函数输出教师和学生的基本信息; 2、计算教师的平均工资并输出; 3、使用友元函数重载运算符“<”,找出入学成绩最高的学生并输出;
时间: 2024-01-25 07:05:16 浏览: 66
C++ 实验六 继承与派生编程
好的,我理解了你的问题。下面是一个 C++ 的实现,实现了你的要求:
```c++
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Person{
public:
Person(string name, string sex, int age):name(name), sex(sex), age(age) {}
virtual void display() const{
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
}
protected:
string name;
string sex;
int age;
};
class Teacher:public Person{
public:
Teacher(string name, string sex, int age, int id, string title, double salary):Person(name, sex, age), id(id), title(title), salary(salary){}
virtual void display() const{
Person::display();
cout << "ID: " << id << endl;
cout << "Title: " << title << endl;
cout << "Salary: " << salary << endl;
}
static double AvgSalary(vector<Teacher> t){
double sum=0.0;
for(vector<Teacher>::iterator it=t.begin(); it!=t.end(); ++it)
sum += it->salary;
return sum/t.size();
}
protected:
int id;
string title;
double salary;
};
class Student:public Person{
public:
Student(string name, string sex, int age, int id, string cls, string major, double score):Person(name, sex, age), id(id), cls(cls), major(major), score(score){}
virtual void display() const{
Person::display();
cout << "ID: " << id << endl;
cout << "Class: " << cls << endl;
cout << "Major: " << major << endl;
cout << "Score: " << score << endl;
}
friend bool operator<(const Student& s1, const Student& s2){
return s1.score < s2.score;
}
protected:
int id;
string cls;
string major;
double score;
};
int main(){
Teacher t1("Tom", "Male", 35, 101, "Professor", 8000);
Teacher t2("Lucy", "Female", 40, 102, "Associate Professor", 6000);
Teacher t3("Jerry", "Male", 30, 103, "Lecturer", 4000);
vector<Teacher> teachers;
teachers.push_back(t1);
teachers.push_back(t2);
teachers.push_back(t3);
cout << "Teachers' information:" << endl;
for(vector<Teacher>::iterator it=teachers.begin(); it!=teachers.end(); ++it)
it->display();
cout << "The average salary of teachers is: " << Teacher::AvgSalary(teachers) << endl;
Student s1("Alice", "Female", 18, 201, "Class 1", "Computer Science", 95);
Student s2("Bob", "Male", 19, 202, "Class 2", "Mathematics", 80);
Student s3("Eva", "Female", 20, 203, "Class 3", "Physics", 90);
vector<Student> students;
students.push_back(s1);
students.push_back(s2);
students.push_back(s3);
cout << "Students' information:" << endl;
for(vector<Student>::iterator it=students.begin(); it!=students.end(); ++it)
it->display();
Student top = students[0];
for(vector<Student>::iterator it=students.begin(); it!=students.end(); ++it)
if(*it < top)
top = *it;
cout << "The student with the highest score is: " << endl;
top.display();
return 0;
}
```
这段代码首先定义了 `Person` 类作为基类,包含了姓名、性别和年龄。然后分别定义了 `Teacher` 类和 `Student` 类,它们都继承自 `Person` 类,并分别增加了教师和学生特有的属性。每个类都实现了虚函数 `display` 来输出基本信息。
在 `Teacher` 类中,我们增加了一个静态函数 `AvgSalary` 来计算平均工资,这个函数接收一个 `vector` 类型的参数,可以方便地计算出所有教师的平均工资。
在 `Student` 类中,我们使用了友元函数 `operator<` 来重载小于号运算符,以便比较学生的入学成绩。
在 `main` 函数中,我们创建了几个教师和学生的实例,并将它们存储在一个 `vector` 中。然后分别输出了教师和学生的基本信息,并计算了教师的平均工资和入学成绩最高的学生。
阅读全文