1.研究生类Student包含属性学号,入学时间,成员函数根据需要自行定义; 2.类Teacher包含属性系部,职称(助教、讲师、教授),工作时间,成员函数根据需要自行定义; 3.由Student和Teacher派生出CGradOnWork在职研究生类,成员函数根据需要自行定义; 4.要求:创建时间类表示入学时间和工作时间,包含年、月两个属性。 5.输入并输出在职研究生的所有信息。 c++
时间: 2024-03-02 08:54:12 浏览: 29
以下是实现上述要求的C++代码:
```c++
#include <iostream>
#include <string>
using namespace std;
// 时间类
class Date {
public:
Date(int y, int m) : year(y), month(m) {}
int year, month;
};
// 学生类
class Student {
public:
string id; // 学号
Date admissionDate; // 入学时间
Student(string i, Date d) : id(i), admissionDate(d) {}
virtual void printInfo() {
cout << "学号:" << id << endl;
cout << "入学时间:" << admissionDate.year << "年" << admissionDate.month << "月" << endl;
}
};
// 教师类
class Teacher {
public:
string department; // 系部
string title; // 职称
Date workDate; // 工作时间
Teacher(string d, string t, Date w) : department(d), title(t), workDate(w) {}
virtual void printInfo() {
cout << "系部:" << department << endl;
cout << "职称:" << title << endl;
cout << "工作时间:" << workDate.year << "年" << workDate.month << "月" << endl;
}
};
// 在职研究生类
class CGradOnWork : public Student, public Teacher {
public:
CGradOnWork(string i, Date d1, string dep, string t, Date d2)
: Student(i, d1), Teacher(dep, t, d2) {}
virtual void printInfo() {
Student::printInfo();
Teacher::printInfo();
}
};
int main() {
// 测试数据
Date date1(2021, 9);
Date date2(2018, 3);
CGradOnWork c("20210101", date1, "计算机科学与技术", "讲师", date2);
c.printInfo();
return 0;
}
```
在本代码中,我们定义了三个类:时间类 `Date`,学生类 `Student`,教师类 `Teacher`,以及一个从学生和教师派生出来的在职研究生类 `CGradOnWork`。其中,时间类 `Date` 包含了年、月两个属性,学生类 `Student` 包含了学号和入学时间属性以及一个打印信息的虚函数,教师类 `Teacher` 包含了系部、职称和工作时间属性以及一个打印信息的虚函数,而在职研究生类 `CGradOnWork` 继承了学生类和教师类的属性和函数。
在 `main` 函数中,我们创建了一个在职研究生对象 `c`,并输出其所有信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)