设计一个老师类teacher,描述老师的信息(姓名,年龄)和职责(学习)再用继承设计一个·学生的一个类Student,描述学生的信息(姓名,学号)和职责(学习),研究生的类,描述研究生的信息(姓名,年龄,学号)和职责(学习,教学)用继承方法,C++
时间: 2024-05-22 22:15:44 浏览: 44
++代码如下:
#include <iostream>
#include <string>
using namespace std;
class Teacher {
protected:
string name;
int age;
public:
Teacher(string n, int a) {
name = n;
age = a;
}
void teach() {
cout << name << " is teaching." << endl;
}
};
class Student : public Teacher {
private:
string stuNum;
public:
Student(string n, int a, string num) : Teacher(n, a) {
stuNum = num;
}
void study() {
cout << name << " is studying." << endl;
}
};
class Graduate : public Student {
public:
Graduate(string n, int a, string num) : Student(n, a, num) {}
void teach() {
cout << name << " is teaching." << endl;
}
};
int main() {
Teacher t("Mr. Li", 35);
t.teach();
Student s("Tom", 18, "1001");
s.teach();
s.study();
Graduate g("Amy", 25, "2001");
g.teach();
g.study();
return 0;
}
阅读全文