多重继承,可以直接输出没有冲突的成员;
代码实现:
#include <iostream>
using namespace std;
class Student
{
public:
Student(){}
Student(string n,int a,int s):name(n),age(a),score(s)
{
this->name=n;
this->age=a;
this->score=s;
}
protected:
string name;
int age;
int score;
};
class Teacher
{
public:
Teacher(){}
Teacher(int a,string p):age(a),position(p)
{
this->age=a;
this->position=p;
}
protected:
int age;
string position;
};
class graduate:public Student,public Teacher
{
public:
graduate(){}
graduate(string n,int a,int s,string p,string r):Student(n,a,s),Teacher(a,p),sex(r){}
void show()
{
cout<<"name:"<<name<<'\t'<<"age:"<<Student::age<<'\t'<<"score:"<<score<<endl;
cout<<"position:"<<position<<endl;
cout<<"sex:"<<sex<<endl;
}
private:
string sex;
};
int main()
{
graduate g1("lisi",19,100,"math teacher","woman");
g1.show();
cout << "Hello World!" << endl;
return 0;
}
实现结果: