本关任务:编写一个输出学生和教师数据的程序,学生数据有编号、姓名、年龄、班号和成绩;教师数据有编号、姓名、年龄、职称和部门。要求声明一个person类,并作为学生数据操作类student和教师数据操作类teacher的基类。
时间: 2024-04-21 19:28:17 浏览: 38
好的,我已经理解了您的要求。下面是一个简单的程序示例,以C++语言为例:
```c++
#include <iostream>
#include <string>
using namespace std;
class person { // person类为基类,包含编号、姓名、年龄等属性
public:
int id;
string name;
int age;
};
class student : public person { // student类继承自person类,增加班号和成绩属性
public:
int classNo;
float score;
};
class teacher : public person { // teacher类继承自person类,增加职称和部门属性
public:
string title;
string department;
};
int main() {
student s;
s.id = 1;
s.name = "张三";
s.age = 18;
s.classNo = 1;
s.score = 90.5;
teacher t;
t.id = 1001;
t.name = "李老师";
t.age = 35;
t.title = "教授";
t.department = "计算机科学与技术";
cout << "学生信息:" << endl;
cout << "编号:" << s.id << endl;
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "班号:" << s.classNo << endl;
cout << "成绩:" << s.score << endl;
cout << endl;
cout << "教师信息:" << endl;
cout << "编号:" << t.id << endl;
cout << "姓名:" << t.name << endl;
cout << "年龄:" << t.age << endl;
cout << "职称:" << t.title << endl;
cout << "部门:" << t.department << endl;
return 0;
}
```
这个程序定义了一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。其中,student类和teacher类都继承自person类,并增加了各自特有的属性。在主函数中,我们创建了一个学生对象s和一个教师对象t,并输出它们的属性信息。
希望这个程序可以帮助您完成本关任务。如果您有任何问题,欢迎随时问我。