定义一个学生类Student和教师类Teacher,学生类的数据成员有姓名、学生、专业,教师类的数据成员有姓名、工作证号、职称、课程、每周课时数。再定义一个助教类TA,继承学生类和教师类,该类可以使用学生类的全部数据成员以及教师类的课程和每周课时数的数据成员。要求:每个类提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。
时间: 2024-02-16 11:00:11 浏览: 126
好的,以下是实现这个要求的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int student_id;
string major;
public:
Student(string n = "", int id = 0, string m = "") : name(n), student_id(id), major(m) {}
~Student() {}
void ShowInfo() {
cout << "姓名:" << name << endl;
cout << "学号:" << student_id << endl;
cout << "专业:" << major << endl;
}
};
class Teacher {
private:
string name;
int work_id;
string title;
string course;
int weekly_hours;
public:
Teacher(string n = "", int id = 0, string t = "", string c = "", int h = 0) :
name(n), work_id(id), title(t), course(c), weekly_hours(h) {}
~Teacher() {}
void ShowInfo() {
cout << "姓名:" << name << endl;
cout << "工作证号:" << work_id << endl;
cout << "职称:" << title << endl;
cout << "课程:" << course << endl;
cout << "每周课时数:" << weekly_hours << endl;
}
};
class TA : public Student, public Teacher {
public:
TA(string n = "", int sid = 0, string m = "", int wid = 0, string t = "", string c = "", int h = 0) :
Student(n, sid, m), Teacher(n, wid, t, c, h) {}
~TA() {}
void ShowInfo() {
Student::ShowInfo();
cout << "课程:" << Teacher::course << endl;
cout << "每周课时数:" << Teacher::weekly_hours << endl;
}
};
int main() {
Student s("张三", 1001, "计算机科学与技术");
s.ShowInfo();
cout << endl;
Teacher t("李四", 2001, "副教授", "数据结构", 6);
t.ShowInfo();
cout << endl;
TA ta("王五", 1002, "软件工程", 2002, "计算机网络", 4);
ta.ShowInfo();
return 0;
}
```
输出结果为:
```
姓名:张三
学号:1001
专业:计算机科学与技术
姓名:李四
工作证号:2001
职称:副教授
课程:数据结构
每周课时数:6
姓名:王五
学号:1002
专业:软件工程
课程:计算机网络
每周课时数:4
```
阅读全文
相关推荐


















