P162 5.18:编辑一个学生和教师数据输入和显示程序,学生数据要编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
#include<iostream>
#include<string>
using namespace std;
class person{
public:
person(string na,int num)
{ name=na;
number=num;}
protected:
string name;
int number;};
class student:virtual public person{
public:
student(string na,int num,int cn,int sc):person(na,num)
{ clanum=cn;
score=sc;}
void sshow()
{ cout<<"姓名:"<<name<<endl;
cout<<"编号:"<<number<<endl;
cout<<"班号:"<<clanum<<endl;
cout<<"成绩:"<<score<<endl;}
protected:
int clanum;
int score;}
;
class teacher:virtual public person{
public:
teacher(string na,int num,string pos,string dep):person(na,num)
{ position=pos;
dept=dep;
}
void tshow()
{ cout<<"姓名:"<<name<<endl;
cout<<"编号:"<<nu

本篇讲述了如何使用C++设计一个`person`基类,包含编号和姓名,并分别派生出`student`和`teacher`类,用于处理学生和教师的数据,包括编号、姓名、班级/职称等信息。此外,还涉及了多继承的概念,通过虚基类`base`派生出`leader`和`engineer`,再进一步派生出`chairman`类,用于模拟组织结构并进行相关数据测试。
最低0.47元/天 解锁文章

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



