Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. Person class has the following members: string name; string address; string phoneNumber; string email; Student class has the following members: enum class_status{frssman,sophomore,junior,senior}; class_status status; Employee class has the following members: string office; int salary; MyDate dateHired; Faculty class has the following members: string officeHours; int rank; Staff class has the following members: string title; MyDate class contains the following members: int year; int month; int day; Override the toString function in each class to display the class name. For example, in Employee class: string toString() const { return "Employee"; } Make sure that the following code: void f(const Person &p) { cout << p.toString() << endl; } Person person; Student student; Employee employee; Faculty faculty; Staff staff; f(person); f(student); f(employee); f(faculty); f(staff); outputs: Person Student Employee Faculty Staff
时间: 2024-02-04 22:04:26 浏览: 53
Person类和它的两个子类Student、Employee
5星 · 资源好评率100%
Sure, here's an implementation of the classes and the toString function:
```c++
#include <iostream>
#include <string>
using namespace std;
class MyDate {
public:
int year;
int month;
int day;
};
class Person {
public:
string name;
string address;
string phoneNumber;
string email;
virtual string toString() const {
return "Person";
}
};
class Student : public Person {
public:
enum class_status { freshman, sophomore, junior, senior };
class_status status;
string toString() const override {
return "Student";
}
};
class Employee : public Person {
public:
string office;
int salary;
MyDate dateHired;
string toString() const override {
return "Employee";
}
};
class Faculty : public Employee {
public:
string officeHours;
int rank;
string toString() const override {
return "Faculty";
}
};
class Staff : public Employee {
public:
string title;
string toString() const override {
return "Staff";
}
};
void f(const Person &p) {
cout << p.toString() << endl;
}
int main() {
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
return 0;
}
```
Output:
```
Person
Student
Employee
Faculty
Staff
```
阅读全文