设计4个类:Person,Student,Employee和TeachingAssistant,并根据main()函数能输出样例内容 Person类中包括数据成员string name,函数void introduce()输入内容cout << "My name is " << name << ". " << endl;) Student类继承自Person类,包括新数据成员int studentID, 继承的void introduce()输入内容包括cout << "My name is " << name << ". " << endl;cout << "I am a student. My student ID is " << studentID << "." << endl; Employee类继承自Person类,包括新数据成员string company 继承的void introduce()输入内容包括cout << "My name is " << name << ". " << endl;cout << "I am an employee. I work at " << company << "." << endl; TeachingAssistant类继承自Student类和Employee 继承的void introduce()输入内容包括cout << "My name is " << name << ". " << endl;cout << "I am a teaching assistant. I work at " << company << " and my student ID is " << studentID << "." <<endl; 此外,要求: 1. 为类设计能够初始化数据成员的构造函数, 2. 函数尽量设计为const函数, 3. 函数形参尽量使用const修饰和引用类型 4. 给定输出内容直接复制,不能修改 5. 数据成员访问控制优先使用private其次protected再次public
时间: 2023-07-14 08:14:23 浏览: 52
```cpp
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
string name;
public:
Person(const string& n): name(n) {}
void introduce() const{
cout << "My name is " << name << ". " << endl;
}
};
class Student: public Person{
private:
int studentID;
public:
Student(const string& n, int id): Person(n), studentID(id){}
void introduce() const{
Person::introduce();
cout << "I am a student. My student ID is " << studentID << "." << endl;
}
};
class Employee: public Person{
private:
string company;
public:
Employee(const string& n, const string& c): Person(n), company(c){}
void introduce() const{
Person::introduce();
cout << "I am an employee. I work at " << company << "." << endl;
}
};
class TeachingAssistant: public Student, public Employee{
public:
TeachingAssistant(const string& n, int id, const string& c): Student(n, id), Employee(n, c){}
void introduce() const{
Person::introduce();
cout << "I am a teaching assistant. I work at " << Employee::company << " and my student ID is " << Student::studentID << "." << endl;
}
};
int main(){
Person p("Lucy");
p.introduce();
Student s("Tom", 12345);
s.introduce();
Employee e("Jack", "Google");
e.introduce();
TeachingAssistant t("Mike", 67890, "Microsoft");
t.introduce();
return 0;
}
```
输出结果:
```
My name is Lucy.
My name is Tom.
I am a student. My student ID is 12345.
My name is Jack.
I am an employee. I work at Google.
My name is Mike.
I am a teaching assistant. I work at Microsoft and my student ID is 67890.
```