题目描述:
编写一个学生和老师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。
要求将编号、姓名、输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
程序代码:
#include<iostream> #include<string> using namespace std; class Person { public: void get() { cout<<"请输入编号:"; cin>>number; cout<<"请输入姓名:"; cin>>name; } void show() { cout<<"NO."<<number<<endl; cout<<"name:"<<name<<endl; } private: string number; string name; }; class Student:public Person { public: void get() { Person::get(); cout<<"请输入班级编号:"; cin>>class_number; cout<<"请输入成绩:"; cin>>grade; } void show() { Person::show(); cout<<"class_number:"<<class_number<<endl; cout<<"grade:"<<grade<<endl; } private: string class_number; float grade; }; class Teacher:public Person { public: void get() { Person::get(); cout<<"请输入职称:"; cin>>title; cout<<"请输入部门:"; cin>>department; } void show() { Person::show(); cout<<"title:"<<title<<endl; cout<<"department:"<<department<<endl; } private: string title; string department; }; int main() { Student s1; Teacher t1; cout<<"输入一个学生数据:"<<endl; s1.get(); cout<<"输出一个学生数据:"<<endl; s1.show(); cout<<"==========================="<<endl; cout<<"输入一个老师数据:"<<endl; t1.get(); cout<<"输出一个老师数据:"<<endl; t1.show(); return 0; }
结果输出:
输入一个学生数据: 请输入编号:001 请输入姓名:qianshou 请输入班级编号:003 请输入成绩:87.5 输出一个学生数据: NO.001 name:qianshou class_number:003 grade:87.5 =========================== 输入一个老师数据: 请输入编号:007 请输入姓名:kkx 请输入职称:professor 请输入部门:seventh 输出一个老师数据: NO.007 name:kkx title:professor department:seventh