类的继承与派生
编写一个能输入、输出学生和教师数据的程序。学生数据有编号、姓名、班号和成绩;教师数据有编号、姓名、职称和部门。声明一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。person类中含有性别、年龄等公有数据成员和Enter( ),display( )等成员函数。
要求:使用对象数组保存输入的对象。在main函数中,调用类中的成员函数录入学生或教师的信息,显示输出学生或教师的信息。
程序代码:/************************************************************************/
/*2、编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、
年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、
姓名、性别、年龄的输入和显示设计成一个类Persona,并作为学生类Student和教师
类Teacher的基类。 */
/************************************************************************/
#include <iostream>
#include <string>
using namespace std;
class Persona
{
public:
int Num;
string Name;
string Sex;
int Age;
void InfoSet(int Num0,string Name0,string Sex0,int Age0)
{
Num=Num0;
Name=Name0;
Sex=Sex0;
Age=Age0;
}
void InfoPrint()
{
cout<<"编号:"<<Num<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"性别:"<<Sex<<endl;
cout<<"年龄:"<<Age<<endl;
}
};
class Student:protected Persona
{
public:
Student(int num,string name,string sex,int age,string m,float s)
{
InfoSet(num,name,sex,age);
major=m;score=s;
}
void StuMajorSet(string mm)
{
major=mm;
}
void StuScoreSet(float ss)
{
score=ss;
}
void StudentPrint()
{
InfoPrint();
cout<<"系别:"<<major<<endl;
cout<<"分数:"<<score<<endl;
}
private:
string major;
float score;
};
class Teacher:protected Persona
{
public:
Teacher(int num,string name,string sex,int age,string j,string d)
{
InfoSet(num,name,sex,age);
job=j;department=d;
}
void TeachJobSet(string jj)
{
job=jj;
}
void TeachDepartSet(float dd)
{
department=dd;
}
void TeacherPrint()
{
InfoPrint();
cout<<"职称:"<<job<<endl;
cout<<"部门:"<<department<<endl;
}
private:
string job;
string department;
};
int main()
{
Student stu(1,"张三","男",20,"计算机系",90);
Teacher teach(2,"无云","女",20,"讲师","计算机系");
cout<<"/////////////////////////////"<<endl;
stu.StudentPrint();
cout<<"/////////////////////////////"<<endl;
teach.TeacherPrint();
cout<<"/////////////////////////////"<<endl;
return 0;
}