作业
定义一个学生类:成员-->姓名、年龄、成绩以及相关函数
定义一个老师类:成员-->年龄、职称以及相关函数
由学生类和老师类共同派生出研究生类:成员-->性别
实例化就研究生类的对象,并输出该对象的所有信息
一、代码
#include <iostream>
#include <string>
using namespace std;
class teacher
{
public:
teacher(){}
~teacher(){}
teacher(int a,string n,string t):age(a),name(n),title(t){}
void show()
{
cout<<"teacher::"<<'\t'<<name<<'\t'<<age<<'\t'<<title<<endl;
}
private:
string title;
protected:
string name;
int age;
};
class student
{
public:
student(){}
~student(){}
student(int a,float s):age(a),score(s){}
void show()
{
cout<<"student::"<<age<<'\t'<<score<<endl;
}
private:
protected:
float score;
int age;
};
class graduate:public teacher,public student
{
public:
graduate(){}
~graduate(){}
graduate(int a,string n,string t,float s,int g):teacher(a,n,t),student(a,s),Gender(g){}
void show()
{
cout<<'\t'<<teacher::name<<'\t'<<student::age<<'\t'<<Gender<<'\t'<<score<<endl;
}
private:
protected:
int Gender;
};
int main()
{
graduate s1(18,"李华","研究生",89,1);
cout<<'\t'<<"姓名"<<'\t'<<"年龄"<<'\t'<<"性别"<<"\t"<<"成绩"<<endl;
s1.show();
cout << "Hello World!" << endl;
return 0;
}
二、执行结果