在一个大学里,有若干教师和学生。根据这个信息设计三个类University、Teacher、Person和Student,一个接口Learnable。要求:
(1) 设计University类,里面使用常量属性:NAME,其值为NUIST。
(2) 设计Person类,属性包括姓名name和年龄age,添加无参和全参构造方法,并封装。
(3) 写出Learnable接口,里面包含study()和exercise()两个方法。
(4) 设计Teacher类(增加工号teacherID属性和工资salary属性)和Student类(增加学号studentID属性和专业major属性),它们继承Person类。
(5) University添加一个数组,里面存有所有教师和学生对象。
该类中有如下方法:
1)void register(Person person) 注册某人
2)void fire(Person person) 开除一个人
3)Person[] searchPerson(String name)根据名字查出所有与此名相同的Person,得到一个数组。
4)double getTotalSalary() 计算大学里面所有教师的工资总和。
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
class Person
{
public:
Person() {}
Person(const std::string &name, int age) : _name(name), _age(age) {}
virtual ~Person() {}
Person(const Person &other) = delete;
Person &operator=(const Person &other) = delete;
const std::string name() const { return _name; }
void setName(const std::string &name) { _name = name; }
int age() const { return _age; }
void setAge(int age) { _age = age; }
private:
std::string _name;
int _age;
};
class Teacher : public Person
{
public:
Teacher() {}
Teacher(const std::string &name, int age, const std::string &teacherID, double salary) : Person(name, age), _teacherID(teacherID), _salary(salary) {}
virtual ~Teacher() {}
const std::string &teacherID() const { return _teacherID; }
void setTeacherID(const std::string &teacherID) { _teacherID = teacherID; }
double salary() const { return _salary; }
void setSalary(double salary) { _salary = salary; }
private:
std::string _teacherID;
double _salary;
};
class Student : public Person
{
public:
Student() {}
Student(const std::string &name, int age, const std::string &studentID, const std::string &major) : Person(name, age), _studentID(studentID), _major(major) {}
virtual ~Student() {}
const std::string &studentID() const { return _studentID; }
void setStudentID(const std::string &studentID) { _studentID = studentID; }
const std::string &major() const { return _major; }
void setMajor(const std::string &major) { _major = major; }
private:
std::string _studentID;
std::string _major;
};
class University
{
public:
University() {}
~University()
{
for (auto p : _persons)
delete p;
}
void registerPerson(Person *person)
{
if (std::find(_persons.begin(), _persons.end(), person) == _persons.end())
_persons.push_back(person);
}
void firePerson(Person *person)
{
auto itr = std::find(_persons.begin(), _persons.end(), person);
if (itr != _persons.end())
_persons.erase(itr);
}
std::vector<Person *> searchPerson(const std::string &name) const
{
std::vector<Person *> persons;
for (auto p : _persons)
if (p->name() == name)
persons.push_back(p);
return persons;
}
double totalSalary() const
{
double sum = 0.0;
for (auto p : _persons)
if (auto t = dynamic_cast<Teacher *>(p))
sum += t->salary();
return sum;
}
private:
std::vector<Person *> _persons;
};
class Learnable
{
public:
virtual ~Learnable() {}
virtual void study() = 0;
virtual void exercise() = 0;
};
int main()
{
University uni;
uni.registerPerson(new Teacher("John Smith", 45, "T1", 50000.0));
uni.registerPerson(new Teacher("Ross Brown", 50, "T2", 65000.0));
uni.registerPerson(new Student("Sara Smith", 21, "S1", "CS"));
// ...
return 0;
}