#include <iostream>
using namespace std;
//代码实现
// 定义一个学生类(Student):保护成员--> 姓名、年龄、成绩以及相关函数
// 定义一个老师类(Teacher):保护成员--> 年龄、职称以及相关函数 、输出函数
// 由学生类和老师类共同派生出研究生类(Graduate):成员--> 性别
// 实例化出研究生类的对象,并输出该对象的所有信息
class student
{
protected:
string name;
int age;
int score;
public:
student(){}
student(string n,int a,int s):name(n),age(a),score(s){}
~student(){}
void show()
{
cout<<"name:"<<name<<" "<<"age:"<<age<<" "<<"score"<<score<<endl;
}
};
class teacher
{
protected:
string title;
int age;
public:
teacher(){}
teacher(string t,int a):title(t),age(a){}
~teacher(){}
void show()
{
cout<<"title:"<<title<<" "<<"age:"<<age<<endl;
}
};
//生产汇聚子类
class graduate:public student,public teacher
{
public:
graduate(){}
graduate(string n,int a,int s,string t,string sex):student(n,a,s),teacher(t,a),sex(sex){}
void show()
{
cout<<"name:"<<name<<" "<<"age:"<<student::age<<" "<<"score:"<<score<<" "<<"title:"<<title<<" "<<"sex:"<<sex<<endl;
}
private:
string sex;
};
int main()
{
graduate g1("zy",22,99,"computer","man");
g1.show();
return 0;
}
定义一个学生类(Student):保护成员--> 姓名、年龄、成绩以及相关函数 定义一个老师类(Teacher):保护成员--> 年龄、职称以及相关函数 、输出函数
最新推荐文章于 2024-08-26 17:31:44 发布