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 Person Student Employee Faculty Staff
时间: 2024-01-11 07:02:35 浏览: 75
Here's an implementation of the Person, Student, Employee, Faculty, Staff, and MyDate classes that satisfies the requirements of the prompt:
```c++
#include <iostream>
#include <string>
using namespace std;
class MyDate {
public:
MyDate(int year, int month, int day)
: year(year), month(month), day(day) {}
string toString() const {
return "MyDate";
}
private:
int year;
int month;
int day;
};
class Person {
public:
Person(string name, string address, string phoneNumber, string email)
: name(name), address(address), phoneNumber(phoneNumber), email(email) {}
virtual string toString() const {
return "Person";
}
protected:
string name;
string address;
string phoneNumber;
string email;
};
enum class class_status {
freshman,
sophomore,
junior,
senior
};
class Student : public Person {
public:
Student(string name, string address, string phoneNumber, string email, class_status status)
: Person(name, address, phoneNumber, email), status(status) {}
string toString() const override {
return "Student";
}
private:
class_status status;
};
class Employee : public Person {
public:
Employee(string name, string address, string phoneNumber, string email, string office, int salary, MyDate dateHired)
: Person(name, address, phoneNumber, email), office(office), salary(salary), dateHired(dateHired) {}
string toString() const override {
return "Employee";
}
protected:
string office;
int salary;
MyDate dateHired;
};
class Faculty : public Employee {
public:
Faculty(string name, string address, string phoneNumber, string email, string office, int salary, MyDate dateHired, string officeHours, int rank)
: Employee(name, address, phoneNumber, email, office, salary, dateHired), officeHours(officeHours), rank(rank) {}
string toString() const override {
return "Faculty";
}
private:
string officeHours;
int rank;
};
class Staff : public Employee {
public:
Staff(string name, string address, string phoneNumber, string email, string office, int salary, MyDate dateHired, string title)
: Employee(name, address, phoneNumber, email, office, salary, dateHired), title(title) {}
string toString() const override {
return "Staff";
}
private:
string title;
};
void f(const Person &p) {
cout << p.toString() << endl;
}
int main() {
Person person("Alice", "123 Main St", "555-1234", "alice@example.com");
Student student("Bob", "456 Elm St", "555-5678", "bob@example.com", class_status::junior);
Employee employee("Charlie", "789 Oak St", "555-9012", "charlie@example.com", "Room 101", 50000, MyDate(2020, 1, 1));
Faculty faculty("Dave", "234 Maple St", "555-3456", "dave@example.com", "Room 202", 70000, MyDate(2010, 1, 1), "MWF 9-11", 2);
Staff staff("Eve", "567 Pine St", "555-7890", "eve@example.com", "Room 303", 40000, MyDate(2015, 1, 1), "Assistant");
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
return 0;
}
```
When you run the code, it outputs:
```
Person
Student
Employee
Faculty
Staff
```
This is the expected output, as it shows that the `toString()` function for each class is correctly overridden to return the name of the class.
阅读全文