【5.18】编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班别和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并将学生数据操作类student和教师数据操作类teacher的基类。
代码:
#include<iostream>
using namespace std;
class person {
public:
void input()
{ cout<<" 编号:"; cin>>no;
cout<<" 姓名: "; cin>>name;
}
void display()
{ cout<<" 编号:"<<no<<endl;
cout<<" 姓名: "<<name<<endl;
}
private:
int no;
char name[10];
};
class student:public person{
public:
void input()
{ person::input();
cout<<" 班号:";
cin>>depart;
cout<<" 成绩:";
cin>>degree;
}
void display()
{ person::disp();
cout<<" 班号:"<<depart<<endl;
cout<<" 成绩:"<<degree<<endl;
}
private:
char depart;
int degree;
};
class teacher:public person{
private:
char prof;
char depart;
public:
void input()
{ person::input();
cout<<" 职称:";
cin>>prof;
cout<<" 部门:";
cin>>depart;
}
void display()
{ person::display();
cout<<" 职称:"<<prof<<endl;
cout<<" 部门:"<<depart<<endl;
}
};
int main()
{ student s1;
teacher t1;
cout<<" 输入一个学生数据:\n";
s1.input();
cout<<" 输入一个教师数据:\n";
t1.input();
cout<<" 显示一个学生数据:\n";
s1.display();
cout<<" 显示一个教师数据:\n";
t1.display();
return 0;
}