实验6 类的继承与派生
一、实验目的
(1)掌握继承和派生的定义,派生类的定义方法和继承方式。
(2)掌握派生类中的构造函数的使用。
(3)掌握多重继承的概念、定义方法、多重继承派生类构造函数的执行顺序。
(4)掌握虚基类的概念和定义方法。
二、实验内容
定义一个基类有姓名、性别、年龄;再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩;由学生类和教师类派生在职研究生类和在读教师类,在职研究生类增加工作单位。
三、实验要求
头文件head.h
#ifndef _IQUERY_H
#define _IQUERY_H 1
#include
class people
{
protected:
string name,sex;
int age;
public:
void input();
void output();
void change(int x);
int search(string pname)const;
int getyear();
};
class teacher:virtual public people
{
private:
string position;
int payment;
protected:
string no;
public:
void input();
void output();
void change(int);
int search2(string pno)const;
};
class student:virtual public people
{
protected:
string no,clas,major;
double score[3];
public:
void input();
void output();
void change(int);
int search1(string pno)const;
};
class master:virtual public teacher,virtual public student
{
private:
string po;
public:
void input();
void output();
void change(int);
};
class stuteacher:virtual public teacher,virtual public student
{
public:
void input();
void output();
void change(int);
};
#endif
成员函数定义文件1.cpp
#include
#include
using namespace std;
#include
int people::getyear()
{
return age;
}
void people::input()
{
cout<<"请输入姓名: ";
cin>>name;
cout<<"请输入性别: ";
cin>>sex;
cout<<"请输入年龄: ";
cin>>age;
}
void people::output()
{
cout<<"姓名: "<<name<<endl;
cout<<"性别: "<<sex<<endl;
cout<<"年龄: "<<age<<endl;
}
void people::change(int x)
{
switch(x)
{
case 1:cin>>name;break;
case 2:cin>>sex;break;
case 3:cin>>age;break;
default:break;
}
}
void student::input()
{
int i;
people::input();
cout<<"请输入学号: ";
cin>>no;
cout<<"请输入班级: ";
cin>>clas;
cout<<"请输入专业: ";
cin>>major;
cout<<"请输入各科成绩: ";
for(i=0;i<3;i++)
cin>>score[i];
}
void student::output()
{
int i;
people::output();
cout<<"学号: "<<no<<endl;
cout<<"班级: "<<clas<<endl;
cout<<"专业: "<<major<<endl;
cout<<"各科成绩: ";
for(i=0;i<3;i++)
cout<<score[i]<<" ";
}
void student::change(int x)
{
int i;
switch(x)
{
case 1:;
case 2:;
case 3:people::change(x);break;
case 4:cin>>no;break;
case 5:cin>>clas;break;
case 6:for(i=0;i<3;i++) cin>>score[i];break;
default:break;
}
}
void teacher::input()
{
people::input();
cout<<"请输入职位: ";
cin>>position;
cout<<"请输入工资: ";
cin>>payment;
cout<<"请输入工号: ";
cin>>no;
}
void teacher::output()
{
people::output();
cout<<"职位: "<<position<<endl;
cout<<"工资: "<<payment<<endl;
cout<<"工号: "<<no<<endl;
}
void teacher::change(int x)
{
switch(x)
{
case 1:;
case 2:;
case 3:people::change(x);break;
case 4:cin>>position;break;
case 5:cin>>payment;break;
case 6:cin>>no;break;
default:break;
}
}
void master::input()
{
int i;
teacher::input();
cout<<"请输入学号: ";
cin>>student::no;
cout<<"请输入班级: ";
cin>>clas;
cout<<"请输入专业: ";
cin>>major;
cout<<"请输入各科成绩: ";
for(i=0;i<3;i++)
cin>>score[i];
cout<<"请输入工作单位: ";
cin>>po;
}
void master::output()
{
int i;
teacher::output();
cout<<"学号: "<<student::no<<endl;
cout<<"班级: "<<clas<<endl;
cout<<"专业: "<<major<<endl;
cout<<"各科成绩: ";
for(i=0;i<3;i++)
cout<<score[i]<<" ";
cout<<endl<<"工作单位: "<<po;
}
void master::change(int x)
{
int i;
switch(x)
{
case 1:;
case 2:;
case 3:;
case 4:;
case 5:;
case 6:teacher::change(x);break;
case 7:cin>>student::no;break;
case 8:cin>>clas;break;
case 9:for(i=0;i<3;i++) cin>>score[i];break;
case 10:cin>>po;
default:break;
}
}
void stuteacher::input()
{
int i;
teacher::input();
cout<<"请输入学号: ";
cin>>student::no;
cout<<"请输入班级: ";
cin>>clas;
cout<<"请输入专业: ";
cin>>major;
cout<<"请输入各科成绩: ";
for(i=0;i<3;i++)
cin>>score[i];
}
void stuteacher::output()
{
int i;
teacher::output();
cout<<"学号: "<<student::no<<endl;
cout<<"班级: "<<clas<<endl;
cout<<"专业: "<<major<<endl;
cout<<"各科成绩: ";
for(i=0;i<3;i++)
cout<<score[i]<<" ";
}
void stuteacher::change(int x)
{
int i;
switch(x)
{
case 1:;
case 2:;
case 3:;
case 4:;
case 5:;
case 6:teacher::change(x);break;
case 7:cin>>student::no;break;
case 8:cin>>clas;break;
case 9:for(i=0;i<3;i++) cin>>score[i];break;
default:break;
}
}
int people::search(string pname)const
{
if(this->name.compare(pname)==0)
return 1;
else return 0;
}
int student::search1(string pno)const
{
if(this->no.compare(pno)==0)
return 1;
else return 0;
}
int teacher::search2(string pno)const
{
if(this->no.compare(pno)==0)
return 1;
else return 0;
}
主函数文件main.cpp
#include
#include
using namespace std;
#define MAX 10
int n1=0,n2=0,n3=0,n4=0,n5=0;
#include
people a[MAX];
student b[MAX];
teacher c[MAX];
master d[MAX];
stuteacher e[MAX];
void mainscreen()
{
cout<<" 信息管理系统 ver0.0.0.1"<<endl;
cout<<"*********************************"<<endl;
cout<<"* a.录入人员信息 *"<<endl;
cout<<"* b.查询人员信息 *"<<endl;
cout<<"* c.显示人员信息 *"<<endl;
cout<<"* d.退出系统 *"<<endl;
cout<<"* e.清屏 *"<<endl;
cout<<"*********************************"<<endl;
}
void in()
{
char w,m;
cout<<"***********************************"<<endl;
cout<<"* a.录入一般人员信息 *"<<endl;
cout<<"* b.录入学生信息 *"<<endl;
cout<<"* c.录入教师信息 *"<<endl;
cout<<"* d.录入在职研究生信息*"<<endl;
cout<<"* e.录入在读教师信息 *"<<endl;
cout<<"***********************************"<<endl;
cout<<" 输入需要的服务类型:";
cin>>w;
do{
if(w=='a')
{
for(;;)
{
a[n1].input();n1++;
cout<<"是否继续输入?是请按y,其余为否。";
cin>>m;
if(m!='y') break;
}
break;
}
if(w=='b')
{
for(;;)
{
b[n2].input();n2++;
cout<<"是否继续输入?是请按y,其余为否。";
cin>>m;
if(m!='y') break;
}
break;
}
if(w=='c')
{
for(;;)
{
c[n3].input();n3++;
cout<<"是否继续输入?是请按y,其余为否。";
cin>>m;
if(m!='y') break;
}
break;
}
if(w=='d')
{
for(;;)
{
d[n4].input();n4++;
cout<<"是否继续输入?是请按y,其余为否。";
cin>>m;
if(m!='y') break;
}
break;
}
if(w=='e')
{
for(;;)
{
e[n5].input();n5++;
cout<<"是否继续输入?是请按y,其余为否。";
cin>>m;
if(m!='y') break;
}
break;
}
}while(w!='a'&&w!='b'&&w!='c'&&w!='d'&&w!='e');
}
void cz()
{
cout<<"*********************************"<<endl;
cout<<"* a.按姓名查询 *"<<endl;
cout<<"* b.按学号查询 *"<<endl;
cout<<"* c.按工号查询 *"<<endl;
cout<<"*********************************"<<endl;
char w;
cin>>w;
int i;
int a1=100,a2=100,a3=100,a4=100,a5=100;
string search;
switch(w)
{
case 'a':
cout<<"请输入要查询的姓名:"<<endl;
cin>>search;
for(i=0;i<=n1;i++)
{
if(a[i].search(search))
{
a[i].output();
a1=i;
}
}
for(i=0;i<=n2;i++)
{
if(b[i].search(search))
{
b[i].output();
a2=i;
}
}
for(i=0;i<=n3;i++)
{
if(c[i].search(search))
{
c[i].output();
a3=i;
}
}
for(i=0;i<=n4;i++)
{
if(d[i].search(search))
{
d[i].output();
a4=i;
}
}
for(i=0;i<=n5;i++)
{
if(e[i].search(search))
{
e[i].output();
a5=i;
}
}
break;
case 'b':
cout<<"请输入要查询的学号:"<<endl;
cin>>search;
for(i=0;i<=n2;i++)
{
if(b[i].search1(search))
{
b[i].output();
a2=i;
}
}
for(i=0;i<=n4;i++)
{
if(d[i].search1(search))
{
d[i].output();
a4=i;
}
}
for(i=0;i<=n5;i++)
{
if(e[i].search1(search))
{
e[i].output();
a5=i;
}
}
break;
case 'c':cout<<"请输入要查询的工号:"<<endl;
cin>>search;
for(i=0;i<=n3;i++)
{
if(c[i].search2(search))
{
c[i].output();
a3=i;
}
}
for(i=0;i<=n4;i++)
{
if(d[i].search2(search))
{
d[i].output();
a4=i;
}
}
for(i=0;i<=n5;i++)
{
if(e[i].search2(search))
{
e[i].output();
a5=i;
}
}
break;
default:break;
}
cout<<"是否要修改信息?按y修改,否则默认不修改,若无信息输出,请无视此提示。";
cin>>w;
if(w=='y')
{
if(a1!=100)
{
cout<<"请输入要修改的项目: "<<endl;
cout<<"1.姓名;"<<endl<<"2.性别;"<<endl<<"3.年龄。"<<endl;
cin>>i;
cout<<"请输入修改后结果:";
a[a1].change(i);
}
if(a2!=100)
{
cout<<"请输入要修改的项目: "<<endl;
cout<<"1.姓名;"<<endl<<"2.性别;"<<endl<<"3.年龄;"<<endl;
cout<<"4.学号;"<<endl<<"5.班级;"<<endl<<"6.成绩。"<<endl;
cin>>i;
cout<<"请输入修改后结果:";
b[a2].change(i);
}
if(a3!=100)
{
cout<<"请输入要修改的项目: "<<endl;
cout<<"1.姓名;"<<endl<<"2.性别;"<<endl<<"3.年龄;"<<endl;
cout<<"4.职位;"<<endl<<"5.工资;"<<endl<<"6.工号。"<<endl;
cin>>i;
cout<<"请输入修改后结果:";
c[a3].change(i);
}
if(a4!=100)
{
cout<<"请输入要修改的项目: "<<endl;
cout<<"1.姓名;"<<endl<<"2.性别;"<<endl<<"3.年龄;"<<endl;
cout<<"4.职位;"<<endl<<"5.工资;"<<endl<<"6.工号;"<<endl;
cout<<"7.学号;"<<endl<<"8.班级;"<<endl<<"9.成绩;"<<endl<<"10.工作单位。"<<endl;
cin>>i;
cout<<"请输入修改后结果:";
d[a4].change(i);
}
if(a5!=100)
{
cout<<"请输入要修改的项目: "<<endl;
cout<<"1.姓名;"<<endl<<"2.性别;"<<endl<<"3.年龄;"<<endl;
cout<<"4.职位;"<<endl<<"5.工资;"<<endl<<"6.工号;"<<endl;
cout<<"7.学号;"<<endl<<"8.班级;"<<endl<<"9.成绩。"<<endl;
cin>>i;
cout<<"请输入修改后结果:";
e[a5].change(i);
}
}
}
void xs()
{
int i;
cout<<"一般人员信息:"<<endl;
for(i=0;i<=n1&&a[i].getyear()!=0;i++)
{
a[i].output();
cout<<endl;
}
cout<<endl<<"学生信息:"<<endl;
for(i=0;i<=n2&&b[i].getyear()!=0;i++)
{
b[i].output();
cout<<endl;
}
cout<<endl<<"教师信息:"<<endl;
for(i=0;i<=n3&&c[i].getyear()!=0;i++)
{
c[i].output();
cout<<endl;
}
cout<<endl<<"在职研究生信息:"<<endl;
for(i=0;i<n4;i++)
{
d[i].output();
cout<<endl;
}
cout<<endl<<"在读教师信息:"<<endl;
for(i=0;i<n5&&e[i].getyear()!=0;i++)
{
e[i].output();
cout<<endl;
}
}
void main()
{
char w;
while(1)
{
mainscreen();
cout<<" 输入需要的服务类型:";
cin>>w;
do{
switch(w)
{
case 'a':in();break;
case 'b':cz();break;
case 'c':xs();break;
case 'd':exit(0);break;
case 'e':system("cls");break;
default:break;
}
}while(w!='a'&&w!='b'&&w!='c'&&w!='d'&&w!='e');
}
}
编译环境:Microsoft Visual C++ 6.0