2019独角兽企业重金招聘Python工程师标准>>>
#include
#include
using namespace std;
class person
{
public:
person(string nam,char se,int ag)
{
name=nam;
sex=se;
age=ag;
}
protected:
string name;
char sex;
int age;
};
class student:virtual public person
{
public:
student(string nam,char se,int ag,string ti):person(nam,se,ag),title(ti){}
protected:
string title;
};
class teacher:virtual public person
{
public:
teacher(string nam,char se,int ag,int sco):person(nam,se,ag),score(sco){}
protected:
int score;
};
class AA:public student,public teacher
{
public:
AA(string nam,char se,int ag,string ti,int sco,int wa):person(nam,se,ag),student(nam,se,ag,ti),teacher(nam,se,ag,sco),wage(wa){
}
void display()
{
cout<<"name:"<
cout<<"sex:"<
cout<<"age:"<
cout<<"title:"<
cout<<"score:"<
cout<<"wage:"<
}
private:
int wage;
};
int main()
{
AA aa("wenming",'m',20,"student",540,2000);
aa.display();
return 0;
}
这个是没有用虚基类代码:
#include
#include
using namespace std;
class person
{
public:
person(string nam,char se,int ag)
{
name=nam;
sex=se;
age=ag;
}
protected:
string name;
char sex;
int age;
};
class student: public person
{
public:
student(string nam,char se,int ag,string ti):person(nam,se,ag),title(ti){}
protected:
string title;
};
class teacher: public person
{
public:
teacher(string nam,char se,int ag,int sco):person(nam,se,ag),score(sco){}
protected:
int score;
};
class AA:public student,public teacher
{
public:
AA(string nam,char se,int ag,string ti,int sco,int wa):student(nam,se,ag,ti),teacher(nam,se,ag,sco),wage(wa){
}
void display()
{
cout<<"name:"<
cout<<"sex:"<
cout<<"age:"<
cout<<"title:"<
cout<<"score:"<
cout<<"wage:"<
}
private:
int wage;
};
int main()
{
AA aa("wenming",'m',20,"student",540,2000);
aa.display();
return 0;
}
转载于:https://my.oschina.net/u/553254/blog/70811