c++ 定义一个学生类Student和教师类Teacher学生类有姓名、学号、私有数据成员教师类有姓名、工作证号、职称、课程、周学时数。再定义一个助教类TA继承学生类和教师类该类可以使用学生类的全部数据成员以及教师类的课程和周学时数的数据成员。要求:每个类提供自定义的构造函数和析构函数并通过同名函数ShowInfo来显示全部数据成员的值
以下是代码实现:
#include
using namespace std;
class Student { private: string name; int id;
public: Student(string name, int id) { this->name = name; this->id = id; }
~Student() {}
void ShowInfo() {
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
}
};
class Teacher { private: string name; int work_id; string title; string course; int weekly_hours;
public: Teacher(string name, int work_id, string title, string course, int weekly_hours) { this->name = name; this->work_id = work_id; this->title = title; this->course = course; this->weekly_hours = weekly_hours; }
~Teacher() {}
void ShowInfo() {
cout << "Name: " << name << endl;
cout << "Work ID: " << work_id << endl;
cout << "Title: " << title << endl;
cout << "Course: " << course << endl;
cout << "Weekly Hours: " << weekly_hours << endl;
}
};
class TA : public Student, public Teacher { public: TA(string name, int id, int work_id, string title, string course, int weekly_hours) : Student(name, id), Teacher(name, work_id, title, course, weekly_hours) {}
~TA() {}
void ShowInfo() {
Student::ShowInfo();
Teacher::ShowInfo();
}
};
int main() { TA ta("John", 123, 456, "Assistant Professor", "Computer Science", 10); ta.ShowInfo(); return 0;
原文地址: https://www.cveoy.top/t/topic/d6X5 著作权归作者所有。请勿转载和采集!