继承和派生5.18

学生数据有编号、姓名、班号和成绩;教师数据有编号、姓名、职称和部门;要求将编号、姓名输入和显示设计成一个person类,并作为student和teacher的基类;

#include<iostream>
#include<string>
using namespace std;
class person{
public:
person()
{
cin>>count;
p=new char[50];
cin>>p;
}
~person()
{
delete p;
}
void show()
{
cout<<count<<" "<<p<<" ";
}
private:
int count;
char *p;
};
class teacher:public person{
public:
teacher()
{
cin>>pos>>mo;
}
void show1()
{
show();
cout<<pos<<" "<<mo<<endl;
}
private:
string pos;
string mo;
};
class student:public person{
public:
student()
{
cin>>num>>score;
}
void show2()
{
show();
cout<<num<<" "<<score<<endl;
}
private:
int num;
int score;
};
int main()
{
student s1;
teacher t1;
s1.show2();
t1.show1();
return 0;
}

原文地址:https://www.cnblogs.com/lanzhiyuan/p/5582811.html