参考前面内容声明Teacher(教师)类,声明Professor(教授)类,是Teacher(教师)类的派生类,新增私有成员projects(项目),另声明类BirthDate(生日)包含year , month , day等数据成员和display函数,生日信息作为Professor类的一个数据成员,并且在Professor中提供一个修改生日的成员函数,在主函数中创建Professor对象,修改生日,并输出其Professor对象所有数据成员。
时间: 2023-12-03 18:42:24 浏览: 114
shiyan.rar_teacher_teacher类
下面是对应的C++代码实现:
```cpp
#include <iostream>
using namespace std;
class BirthDate {
public:
int year;
int month;
int day;
void display() {
cout << year << "-" << month << "-" << day << endl;
}
};
class Teacher {
public:
string name;
int age;
string gender;
Teacher(string n, int a, string g) : name(n), age(a), gender(g) {}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
class Professor : public Teacher {
private:
string projects;
BirthDate birthday;
public:
Professor(string n, int a, string g, string p, BirthDate b) : Teacher(n, a, g), projects(p), birthday(b) {}
void setBirthday(int y, int m, int d) {
birthday.year = y;
birthday.month = m;
birthday.day = d;
}
void display() {
Teacher::display();
cout << "Projects: " << projects << endl;
cout << "Birthday: ";
birthday.display();
}
};
int main() {
BirthDate bday = {1990, 7, 1};
Professor p("Tom", 30, "Male", "AI Research", bday);
p.display();
cout << "After modifying the birthday: " << endl;
p.setBirthday(1985, 1, 15);
p.display();
return 0;
}
```
运行结果:
```
Name: Tom
Age: 30
Gender: Male
Projects: AI Research
Birthday: 1990-7-1
After modifying the birthday:
Name: Tom
Age: 30
Gender: Male
Projects: AI Research
Birthday: 1985-1-15
```
阅读全文