5.18编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称、部门。要求将编号、姓名和显示设计成一个类person,并作为学生数据操作类student和teacher的基类。
#include<iostream>
#include<string>
using namespace std;
class person{
public:
person(string i,string n)
{
id=i;
name=n;
}
void person_show()
{
cout<<"number="<<id<<endl;
cout<<"name="<<name<<endl;
}
private:
string id;
string name;
};
class teacher:public person{
public:
teacher(string i,string n):person(i,n)
{
cout<<"teacher:"<<endl;
person::person_show();
cout<<"title:";
cin>>title;
cout<<"department:";
cin>>department;
}
void teacher_show()
{
person::person_show();
cout<<"title:"<<title<<endl;
cout<<"department:"<<department<<endl;
}
private:
string title;
string department;
};
class student:public person{
public:
student(string i,string n):person(i,n)
{
cout<<"student:"<<endl;
person::person_show();
cout<<"class_no:";
cin>>class_no;
cout<<"score:";
cin>>score;
}
void student_show()
{
person::person_show();
cout<<"class_no:"<<class_no<<endl;
cout<<"score:"<<score<<endl;
}
private:
string class_no;
float score;
};
int main()
{
teacher stu("001","mingge");
student stu1("002","liyi");
cout<<"*****************************"<<endl;
stu.teacher_show();
stu1.student_show();
return 0;
}