代码演示
#include <iostream>
using namespace std;
class stu
{
protected:
string name;
int age;
double socre;
public:
stu(){cout<<"teach::无参构造"<<endl;}
stu(string a,int b,double c):name(a),age(b),socre(c){cout<<"stu::有参构造"<<endl;}
void show()
{
cout<<"stu::name = "<<name<<endl;
cout<<"stu::age = "<<age<<endl;
cout<<"stu::socre = "<<socre<<endl<<endl;
}
};
class teach
{
protected:
int age;
string job;
public:
teach(){cout<<"teach::无参构造"<<endl;}
teach(int a,string b):age(a),job(b){cout<<"teach::有参构造"<<endl;}
void show()
{
cout<<"teach::age = "<<age<<endl;
cout<<"teach::job = "<<job<<endl<<endl;
}
};
//声明grate类,由stu类和teach类共同派生出来
class grate:public stu,public teach
{
public:
char sex;
grate(){cout<<"grate::无参构造"<<endl;}
grate(string a,int b,double c,int e,string f,char g):stu(a,b,c),teach(e,f),sex(g)
{
cout<<"grate::有参构造"<<endl;
}
void show()
{
cout<<"stu::name = "<<name<<endl;
cout<<"stu::age = "<<stu::age<<endl;
cout<<"stu::socre = "<<socre<<endl;
cout<<"teach::age = "<<teach::age<<endl;
cout<<"teach::job = "<<job<<endl;
cout<<"grate::sex = "<<sex<<endl;
}
};
int main()
{
grate g1("bill",22,99.9,28,"C++",'M');
g1.show();
return 0;
}
运行示例