一个学校系统中有教师工和学生,学生又包括本科生、研究生等等。他们有相同的属性,比如姓名、性别、出生日期等,也有一些各自特有的属性,比如学生有学生特有的专业,教师工有特有的职称,首先用一个Person类,将共有的属性放在Person类中,再单独用Student类和Teacher类作为Person类的派生类,这样Student和Teacher不用重复写共同的属性,减少了代码量。
除了Person类、Student类、Teacher类外,还定义一个Interface类,用作用户的页面接口,成员包括stu和tea,以及对stu和tea的直接操作。
“people.h” //头文件和类的声明
#include <iostream>
#include <string>
using namespace std;
const int N=10;
class Date //Date类
{
protected:
int year; //年
int month; //月
int day; //日
public:
void Set(int x,int y,int z); //设置年月日
void Display(); //展示日期
};
class Person //Person继承类
{
protected:
string name; //姓名
string number; //学号或教师工号
char sex; //性别
Date birthday; //出生日期
public:
string Getname(); //提取名字
void Input(); //录入
void Output(); //展示
};
class Student :public Person //Student派生类
{
protected:
string speciality; //专业
public:
void Input(); //录入
void Output(); //展示
};
class Graduate :public Person //Graduate派生类
{
protected:
string researchTopic; //研究课题
public:
void Input(); //录入
void Output(); //展示
};
class Teacher :public Person //Teacher派生类
{
protected:
string academicTitle; //职称
public:
void Input(); //录入
void Output(); //展示
};
class Interface //用户界面
{
protected:
Student stu[N]; //学生
int numStu; //学生人数
Graduate gra[N]; //研究生
int numGra; //研究生人数
Teacher tea[N]; //教师工
int numTea; //教师工人数
public:
Interface(); //给 num 赋初值
void menu(); //菜单
void Input(); //录入信息页面
void Output(); //展示信息页面
};
“peoplemessage.h” //类的定义
#include "people.h"
void Date::Set(int x,int y,int z) //设置年月日
{
year=x;
month=y;
day=z;
}
void Date::Display() //展示
{
printf("%d-%d-%d",year,month,day);
}
string Person::Getname() //提取名字
{
return name;
}
void Person::Output() //展示Person类信息
{
cout<<"姓 名:"<<name<<endl;
cout<<"性 别:"<<sex<<endl;
cout<<"出生日期:";
birthday.Display();
cout<<endl;
cout<<"编 号:"<<number<<endl;
}
void Person::Input() //录入Person类信息
{
cout<<"输入姓名:";
cin>>name;
cout<<"输入性别(M or F):";
cin>>sex;
cout<<"输入出生日期(xxxx-xx-xx):";
int x,y,z;
scanf("%d-%d-%d",&x,&y,&z);
birthday.Set(x,y,z);
cout<<"输入编号:";
cin>>number;
}
void Student::Output() //展示Student类信息
{
Person::Output();
cout<<"专 业:"<<speciality<<endl;
}
void Student::Input() //录入Student类信息
{
Person::Input();
cout<<"输入学生专业:";
cin>>speciality;
}
void Graduate::Output() //显示Graduate类信息
{
Person::Output();
cout<<"研究课题:"<<researchTopic<<endl;
}
void Graduate::Input() //录入Graduate类信息
{
Person::Input();
cout<<"输入研究生研究课题:";
cin>>researchTopic;
}
void Teacher::Output() //展示Teacher类信息
{
Person::Output();
cout<<"教师工职称:"<<academicTitle<<endl;
}
void Teacher::Input() //录入教师工信息
{
Person::Input();
cout<<"输入教师工职称:";
cin>>academicTitle;
}
Interface::Interface() //构造函数
{
numStu=0;
numGra=0;
numTea=0;
}
void Interface::menu() //菜单函数
{
cout<<"******1.录 入*******"<<endl;
cout<<"******2.浏 览*******"<<endl;
cout<<"******0.退 出*******"<<endl;
}
void Interface::Input() //录入信息
{
int type;
char ch;
do {
cout<<"你输入的人员类型(1-学生 2-研究生 3-教师工 0-退出)"<<endl;
cin>>type;
if (type>=1&&type<=3){
if (type==1){
if (numStu==N)
cout<<"人员已满,无法继续输入"<<endl;
else
for (int i=0;i<N;i++){
if (stu[i].Getname().empty())
{
stu[i].Input();
numStu++;
break;
}
}
}
if (type==2){
if (numGra==N)
cout<<"人员已满,无法继续输入"<<endl;
else
for (int i=0;i<N;i++){
if (gra[i].Getname().empty())
{
gra[i].Input();
numGra++;
break;
}
}
}
if (type==3){
if (numTea==N)
cout<<"人员已满,无法继续输入"<<endl;
else
for (int i=0;i<N;i++){
if (tea[i].Getname().empty())
{
tea[i].Input();
numTea++;
break;
}
}
}
}
cout<<"是否继续输入(Y or N)"<<endl;
cin>>ch;
} while (ch == 'Y');
}
void Interface::Output() //浏览信息
{
cout<<"学生信息:"<<endl;
if (numStu == 0)
cout<<"无学生信息!"<<endl;
else
for (int i=0;!stu[i].Getname().empty();i++)
stu[i].Output();
cout<<"研究生信息:"<<endl;
if (numGra == 0)
cout<<"无研究生信息!"<<endl;
else
for (int i=0;!gra[i].Getname().empty();i++)
gra[i].Output();
cout<<"教师工信息:"<<endl;
if (numTea == 0)
cout<<"无教师工信息!"<<endl;
else
for (int i=0;!tea[i].Getname().empty();i++)
tea[i].Output();
}
“people.cpp” //c文件,主函数
#include "stu_message.h"
int main()
{
int choice;
Interface in;
do
{
in.menu();
cin>>choice;
switch(choice){
case 1:
in.Input();break;
case 2:
in.Output();break;
default:break;
}
} while (choice != 0);
system("pause");
return 0;
}