#include #include using namespace std;

class Teacher { public: Teacher(string name, int age) { this->name = name; this->age = age; } void teach() { cout << 'My name is '' << name << '', I am '' << age << '' years old. My responsibility is teaching.' << endl; } private: string name; int age; };

class Student { public: Student(string name, int studentID) { this->name = name; this->studentID = studentID; } void study() { cout << 'My name is '' << name << '', My student ID is '' << studentID << '. My responsibility is studying.' << endl; } private: string name; int studentID; };

class Graduate : public Teacher, public Student { public: Graduate(string name, int age, int studentID) : Teacher(name, age), Student(name, studentID) { this->name = name; this->age = age; this->studentID = studentID; } void teachAndStudy() { cout << 'My name is '' << name << '', I am '' << age << '' years old. My student ID is '' << studentID << '. My responsibility is teaching and studying.' << endl; } private: string name; int age; int studentID; };

int main() { Teacher teacher('John', 40); teacher.teach();

Student student('Tom', 123456);
student.study();

Graduate graduate('Alice', 25, 654321);
graduate.teachAndStudy();

return 0;

}

C++ 实现多重角色类:老师、学生、研究生

原文地址: http://www.cveoy.top/t/topic/f33W 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录