代码实现 定义一个学生类:成员--> 姓名、年龄、成绩以及相关函数 定义一个老师类:成员--> 年龄、职称以及相关函数 由学生类和老师类共同派生出研究生类:成员--> 性别 实例化就研究生类的对象,并输出该对象的所有信息
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(string n,int a,int s):name(n),age(a),score(s){}
void show()
{
cout<<name<<'t'<<age<<'t'<<score<<endl;
}
protected:
string name;
int age;
int score;
};
class teacher
{
public:
teacher(int a,string ssd):age(a),ssd(ssd){}
void show()
{
cout<<age<<'t'<<ssd<<endl;
}
protected:
int age;
string ssd;
};
class moster:public student,public teacher
{
protected:
string sex;
public:
moster(string n,int a,int s,string ssd,string sex):student(n,a,s),teacher(a,ssd),sex(sex){}
void show()
{
cout<<student::name<<'\t'<<student::age<<'\t'<<student::score<<'\t'<<teacher::ssd<<'\t'<<sex<<'t'<<endl;
}
};
int main(int argc, const char *argv[])
{
moster m1("gaofeng",21,99,"docter","man");
m1.show();
return 0;
}
运行图: