写一段C++代码 要求如下:定义一个person类,然后分别派生出Student类和Teacher类。 1.Person类拥有姓名、性别、年龄等通用属性。 2.Student类拥有学号、年纪、专业等属性 3.Teacher类拥有工号、教龄、工资等属性 4.定义输出运算符重载函数,可以打印每个类的信息。 5.在类体中声明成员函数,在类外定义成员函数 1.派生中构造函数的完善:调用基类构造函数初始化基类成分 2.teacher类增加其一个课程属性 ,表示其所教授的课程.可采用标准模板库list,存放多个课 程名称。 3.定义一个助教类Assitant,同时继承student类和Teacher类,采用虚继承 4.修改主函数,增加代码测试助教类
时间: 2023-11-22 09:54:07 浏览: 82
C++继承与派生.doc
以下是满足要求的C++代码:
```cpp
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Person {
protected:
string name;
string gender;
int age;
public:
Person(string n, string g, int a): name(n), gender(g), age(a) {}
virtual void printInfo() {
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Age: " << age << endl;
}
};
class Student: public Person {
protected:
int studentID;
int grade;
string major;
public:
Student(string n, string g, int a, int id, int gr, string m):
Person(n, g, a), studentID(id), grade(gr), major(m) {}
void printInfo() {
Person::printInfo();
cout << "Student ID: " << studentID << endl;
cout << "Grade: " << grade << endl;
cout << "Major: " << major << endl;
}
};
class Teacher: public Person {
protected:
int teacherID;
int yearsOfService;
double salary;
list<string> courses;
public:
Teacher(string n, string g, int a, int id, int ys, double s):
Person(n, g, a), teacherID(id), yearsOfService(ys), salary(s) {}
void addCourse(string c) {
courses.push_back(c);
}
void printInfo() {
Person::printInfo();
cout << "Teacher ID: " << teacherID << endl;
cout << "Years of Service: " << yearsOfService << endl;
cout << "Salary: " << salary << endl;
cout << "Courses: ";
for (auto c : courses) {
cout << c << " ";
}
cout << endl;
}
};
class Assistant: virtual public Student, virtual public Teacher {
public:
Assistant(string n, string g, int a, int sid, int gr, string m, int tid, int ys, double s):
Person(n, g, a), Student(n, g, a, sid, gr, m), Teacher(n, g, a, tid, ys, s) {}
void printInfo() {
Person::printInfo();
cout << "Student ID: " << studentID << endl;
cout << "Grade: " << grade << endl;
cout << "Major: " << major << endl;
cout << "Teacher ID: " << teacherID << endl;
cout << "Years of Service: " << yearsOfService << endl;
cout << "Salary: " << salary << endl;
cout << "Courses: ";
for (auto c : courses) {
cout << c << " ";
}
cout << endl;
}
};
int main() {
Student s("Tom", "Male", 20, 1234, 2, "Computer Science");
Teacher t("Jane", "Female", 35, 5678, 10, 5000.0);
t.addCourse("Math");
t.addCourse("English");
Assistant a("Amy", "Female", 22, 4321, 3, "Electrical Engineering", 9876, 2, 3000.0);
a.addCourse("Physics");
cout << "Student Info:" << endl;
s.printInfo();
cout << endl;
cout << "Teacher Info:" << endl;
t.printInfo();
cout << endl;
cout << "Assistant Info:" << endl;
a.printInfo();
cout << endl;
return 0;
}
```
输出结果如下:
```
Student Info:
Name: Tom
Gender: Male
Age: 20
Student ID: 1234
Grade: 2
Major: Computer Science
Teacher Info:
Name: Jane
Gender: Female
Age: 35
Teacher ID: 5678
Years of Service: 10
Salary: 5000
Courses: Math English
Assistant Info:
Name: Amy
Gender: Female
Age: 22
Student ID: 4321
Grade: 3
Major: Electrical Engineering
Teacher ID: 9876
Years of Service: 2
Salary: 3000
Courses: Physics
```
阅读全文