在这里插//代码实现
//定义一个学生类(Student):保护成员-->姓名、年龄、成绩以及相关函数
//定义一个老师类(Teacher):保护成员-->年龄、职称以及相关函数 、输出函数
//由学生类和老师类共同派生出研究生类(Graduate):成员-->性别
//实例化出研究生类的对象,并输出该对象的所有信息
#include <iostream>
using namespace std;
class stu {
protected:
string name;
int age;
int score;
public:
stu(){cout<<"stu调用无参构造"<<endl;}
stu(string a,int b,int c):name(a),age(b),score(c){cout<<"stu调用有参构造"<<endl;}
stu(const stu &other):name(other.name),age(other.age),score(other.score){
cout<<"stu调用拷贝构造"<<endl;
}
~stu(){cout<<"stu调用析构函数"<<endl;}
};
class tcr {
protected:
string job;
int age;
public:
tcr(){cout<<"tcr调用无参构造"<<endl;}
tcr(string a,int b):job(a),age(b){cout<<"tcr调用有参构造"<<endl;}
tcr(const tcr &other):job(other.job),age(other.age){
cout<<"tcr调用拷贝构造"<<endl;
}
~tcr(){cout<<"tcr调用析构函数"<<endl;}
};
class Graduate:public stu,public tcr{
private:
string sex;
public:
Graduate(){cout<<"graduate调用无参构造"<<endl;}
Graduate(string a,int b,int c,string d,int e,string f):stu(a,b,c),tcr(d,e),sex(f)
{cout<<"Graduate调用有参构造"<<endl;}
~Graduate(){cout<<"graduate调用析构函数"<<endl;}
void show(){
cout<<"stu::name="<<stu::name<<endl;
cout<<"stu::age="<<stu::age<<endl;
cout<<"stu::score="<<stu::score<<endl;
cout<<"tcr::job="<<tcr::job<<endl;
cout<<"tcr::age="<<tcr::age<<endl;
cout<<"Graduate::sex="<<Graduate::sex<<endl;
}
};
int main()
{
Graduate t1("zs",18,100,"xz",35,"男");
t1.show();
return 0;
}
入代码片