#define _CRT_SECURE_NO_WARNINGS
/*
12.4 设计评选优秀教师和学生的程序,其类结构如图 12.5 所示。当输入一系列教师
或学生的记录后,将优秀学生及教师的姓名列出来。
图 12.5 类结构
类 base
char name[8];
含虚函数 isgood() 姓名
类 student
如果考试成绩超过 90 分,
则 isgood()返回 True
类 teacher
如果一年发表的论文超过 3 篇,
则 isgood()返回 True
*/
#include<iostream>
#include <math.h>
using namespace std;
class base
{
public:
char name[8];
bool virtual isgood() = 0;
void virtual getname() = 0;
};
class student:public base
{
int score;
public:
student(char n[], int s)
{
strcpy(name, n);
score = s;
}
void getname()
{
cout << name <<" "<< endl;
}
bool isgood()
{
if (score > 90)
{
return true;
}
return false;
}
};
class teacher :public base
{
int paper;
public:
teacher(char n[], int p)
{
strcpy(name, n);
paper = p;
}
void getname()
{
cout << name << " " << endl;
}
bool isgood()
{
if (paper > 3)
{
return true;
}
return false;
}
};
void main()
{
base *p;
char *a = "xiaohua";
char *b = "xiaohu";
char *t1 = "liutea";
char *t2 = "litea";
student s(a,88);
p = &s;
if (p->isgood()){ p->getname(); }
student s1(b, 92);
p = &s1;
if (p->isgood()){ p->getname(); }
teacher te1(t1, 2);
p = &te1;
if (p->isgood()){ p->getname(); }
teacher te2(t2, 4);
p = &te2;
if (p->isgood()){ p->getname(); }
system("pause");
}
/*
12.4 设计评选优秀教师和学生的程序,其类结构如图 12.5 所示。当输入一系列教师
或学生的记录后,将优秀学生及教师的姓名列出来。
图 12.5 类结构
类 base
char name[8];
含虚函数 isgood() 姓名
类 student
如果考试成绩超过 90 分,
则 isgood()返回 True
类 teacher
如果一年发表的论文超过 3 篇,
则 isgood()返回 True
*/
#include<iostream>
#include <math.h>
using namespace std;
class base
{
public:
char name[8];
bool virtual isgood() = 0;
void virtual getname() = 0;
};
class student:public base
{
int score;
public:
student(char n[], int s)
{
strcpy(name, n);
score = s;
}
void getname()
{
cout << name <<" "<< endl;
}
bool isgood()
{
if (score > 90)
{
return true;
}
return false;
}
};
class teacher :public base
{
int paper;
public:
teacher(char n[], int p)
{
strcpy(name, n);
paper = p;
}
void getname()
{
cout << name << " " << endl;
}
bool isgood()
{
if (paper > 3)
{
return true;
}
return false;
}
};
void main()
{
base *p;
char *a = "xiaohua";
char *b = "xiaohu";
char *t1 = "liutea";
char *t2 = "litea";
student s(a,88);
p = &s;
if (p->isgood()){ p->getname(); }
student s1(b, 92);
p = &s1;
if (p->isgood()){ p->getname(); }
teacher te1(t1, 2);
p = &te1;
if (p->isgood()){ p->getname(); }
teacher te2(t2, 4);
p = &te2;
if (p->isgood()){ p->getname(); }
system("pause");
}
本文介绍了一个使用C++实现的程序设计案例,该程序能够通过继承和多态特性来判断并展示优秀教师与学生的名单。具体而言,通过设定学生考试成绩高于90分和教师年度发表论文数量超过3篇的标准,利用基类的虚函数实现多态调用。
861

被折叠的 条评论
为什么被折叠?



