用C++完成以下要求:Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophonore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Define a constant virtual toString function in the Person class and override it in each class to display the class name and the person’s name. Implement the classes. Write a test program that creates a Person, Student , Employee, Faculty, and Staff, and invokes their toString() function.
时间: 2023-07-02 13:08:17 浏览: 141
以下是用C++实现的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class MyDate {
private:
int year, month, day;
public:
MyDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
int getYear() {
return year;
}
int getMonth() {
return month;
}
int getDay() {
return day;
}
};
class Person {
private:
string name, address, phone, email;
public:
Person(string n, string a, string p, string e) {
name = n;
address = a;
phone = p;
email = e;
}
virtual string toString() const {
return "Person: " + name;
}
};
class Student : public Person {
private:
string classStatus;
public:
Student(string n, string a, string p, string e, string c) : Person(n, a, p, e) {
classStatus = c;
}
virtual string toString() const {
return "Student: " + Person::toString();
}
};
class Employee : public Person {
private:
string office;
double salary;
MyDate dateHired;
public:
Employee(string n, string a, string p, string e, string o, double s, MyDate d) : Person(n, a, p, e) {
office = o;
salary = s;
dateHired = d;
}
virtual string toString() const {
return "Employee: " + Person::toString();
}
};
class Faculty : public Employee {
private:
string officeHours;
string rank;
public:
Faculty(string n, string a, string p, string e, string o, double s, MyDate d, string oh, string r) : Employee(n, a, p, e, o, s, d) {
officeHours = oh;
rank = r;
}
virtual string toString() const {
return "Faculty: " + Person::toString();
}
};
class Staff : public Employee {
private:
string title;
public:
Staff(string n, string a, string p, string e, string o, double s, MyDate d, string t) : Employee(n, a, p, e, o, s, d) {
title = t;
}
virtual string toString() const {
return "Staff: " + Person::toString();
}
};
int main() {
MyDate hireDate(2020, 1, 1);
Person person("John Smith", "123 Main St.", "555-1234", "jsmith@example.com");
Student student("Jane Doe", "456 Elm St.", "555-5678", "jdoe@example.com", "sophomore");
Employee employee("Bob Johnson", "789 Oak St.", "555-9012", "bjohnson@example.com", "101", 50000, hireDate);
Faculty faculty("Alice Brown", "321 Pine St.", "555-3456", "abrown@example.com", "102", 75000, hireDate, "MWF 10-12", "Professor");
Staff staff("Tom Davis", "654 Cedar St.", "555-7890", "tdavis@example.com", "103", 40000, hireDate, "Administrative Assistant");
cout << person.toString() << endl;
cout << student.toString() << endl;
cout << employee.toString() << endl;
cout << faculty.toString() << endl;
cout << staff.toString() << endl;
return 0;
}
```
输出结果为:
```
Person: John Smith
Student: Person: Jane Doe
Employee: Person: Bob Johnson
Faculty: Person: Alice Brown
Staff: Person: Tom Davis
```
阅读全文
相关推荐


















