设计评选优秀教师和优秀学生候选人的程序。如果学生的分数大于90,则可评为优秀生;如果教师发
表的论文数大于 3,则可评为优秀教师。具体要求如下:
(1)定义基类Base:①保护数据成员
char name[8]; //存放姓名
int num; //存放分数或论文数
②公有成员函数
Base():构造函数,输入姓名;
void print):功能函数,输出数据成员;
virtual int Isgood()=0:纯虚函数,用于判断是否满足条件;
(2)由基类派生学生类Student,定义公有成员函数:
Student():构造函数,输入分数;
int Isgood():根据优秀学生的标准,满足条件返回1,否则返回0;
(3)由基类派生教师类Teacher,定义公有成员函数:
Teacher():构造函数,输入论文数;
int Isgood():根据优秀教师的标准,满足条件返回1,否则返回0;
(4)在主函数中定义学生数组和教师数组,输入一系列教师或学生的记录后,将优秀教师及学生候选
人的情况列出来,并体现运行的多态性
#include<iostream>
using namespace std;
class base
{
protected:
char name[8];
int num;
public:
base()
{
cout << "输入姓名:" << endl;
cin >> name;
}
void print()
{
cout << "姓名:" << this->name << " " << "数字:" << this->num << endl;
}
virtual int isgood() = 0;
};
class student :public base
{
public:
student()
{
cout << "输入分数:" << endl;
cin >> num;
}
void print()
{
cout << "姓名:" << this->name << " " << "分数:" << this->num << endl;
}
virtual int isgood()
{
if (this->num >= 90)
return 1;
else
return 0;
}
};
class teacher :public base
{
public:
teacher()
{
cout << "输入论文数:" << endl;
cin >> num;
}
void print()
{
cout << "姓名:" << this->name << " " << "论文数目:" << this->num << endl;
}
virtual int isgood()
{
if (this->num >= 3)
return 1;
else
return 0;
}
};
int main()
{
student stu[3];
student* ps = stu;
int i = 0;
for (i=0,ps=stu;i<3;i++, ps++)
{
if (ps->isgood())
ps->print();
}
teacher tea[4];
teacher* pt = tea;
for (i = 0, pt = tea; i < 4; i++, pt++)
{
if (pt->isgood())
pt->print();
}
system("pause");
}