酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用c++建立一个简单的大学管理系统。 其中有学生和职工,职工中有教师,学生中有助教。 person虚基类 姓名(name), 唯一标识符(id),输出函数(show) employee类 新增:部门(dept),薪水(salary),输出函数(show) student类 新增:专业(major),年级(level),输出函数(show) teacher类 新增:职称(title),输出函数(show) assistant类 新增:输出函数(show) 要求:teacher类继承职工类,assistant类继承employee类和student类,employee类继承person虚基类,student继承person虚基类 在main函数中使用这些类。

首先,我们需要创建一个虚基类Person,包含姓名(name)和唯一标识符(id)属性,以及一个输出函数(show)。 ```cpp #include <iostream> #include <string> class Person { public: Person(const std::string& name, int id) : name_(name), id_(id) {} virtual void show() const = 0; protected: std::string name_; int id_; }; ``` 接下来,我们创建Employee类,继承自Person类,并添加部门(dept)和薪水(salary)属性,以及一个输出函数(show)。 ```cpp class Employee : public Person { public: Employee(const std::string& name, int id, const std::string& dept, double salary) : Person(name, id), dept_(dept), salary_(salary) {} void show() const override { std::cout << "Employee: " << name_ << ", ID: " << id_ << ", Department: " << dept_ << ", Salary: " << salary_ << std::endl; } private: std::string dept_; double salary_; }; ``` 然后,我们创建Student类,继承自Person类,并添加专业(major)和年级(level)属性,以及一个输出函数(show)。 ```cpp class Student : public Person { public: Student(const std::string& name, int id, const std::string& major, int level) : Person(name, id), major_(major), level_(level) {} void show() const override { std::cout << "Student: " << name_ << ", ID: " << id_ << ", Major: " << major_ << ", Level: " << level_ << std::endl; } private: std::string major_; int level_; }; ``` 接下来,我们创建Teacher类,继承自Employee类,并添加职称(title)属性,以及一个输出函数(show)。 ```cpp class Teacher : public Employee { public: Teacher(const std::string& name, int id, const std::string& dept, double salary, const std::string& title) : Employee(name, id, dept, salary), title_(title) {} void show() const override { std::cout << "Teacher: " << name_ << ", ID: " << id_ << ", Department: " << dept_ << ", Salary: " << salary_ << ", Title: " << title_ << std::endl; } private: std::string title_; }; ``` 我们创建Assistant类,继承自Employee类和Student类,并添加一个输出函数(show)。 ```cpp class Assistant : public Employee, public Student { public: Assistant(const std::string& name, int id, const std::string& dept, double salary, const std::string& major, int level) : Employee(name, id, dept, salary), Student(name, id, major, level) {} void show() const override { std::cout << "Assistant: " << name_ << ", ID: " << id_ << ", Department: " << dept_ << ", Salary: " << salary_ << ", Major: " << major_ << ", Level: " << level_ << std::endl; } }; ``` 在main函数中使用这些类: ```cpp int main() { Person p("John Doe", 1); Employee e("Jane Smith", 2, "HR", 50000); Student s("Alice Johnson", 3, "Computer Science", 2); Teacher t("Bob Brown", 4, "Mathematics", 60000, "Professor"); Assistant a("Charlie Green", 5, "Physics", 40000, "Mechanics", 3); p.show(); e.show(); s.show(); t.show(); a.show(); return 0; } ``` [2024-05-29 18:22:53 | AI写代码神器 | 1014点数解答]

相关提问