定义一个学生类:成员-->姓名、年龄、成绩以及相关函数
定义一个老师类:成员-->年龄、职称以及相关函数
由学生类和老师类共同派生出研究生类:成员-->性别
实例化就研究生类的对象,并输出该对象的所有信息
#include <iostream>
using namespace std;
class student
{
protected:
string name;
int age;
float score;
public:
student(){};
student(string buf,int a,float b):name(buf),age(a),score(b){}
student &operator=(student&r)
{
this->name = r.name;
this->age = r.age;
this->score = r.score;
return *this;
}
void show()
{
cout<<name<<endl;
cout<<age<<endl;
cout<<score<<endl;
}
};
class teacher
{
protected:
int age;
string job;
public:
teacher(){}
teacher(int a,string b):age(a),job(b){}
teacher&operator=(teacher&r)
{
this->age = r.age;
this->job = r.job;
return *this;
}
void show()
{
cout<<age<<endl;
cout<<job<<endl;
}
};
class postgraduate:public student,public teacher
{
protected:
string sex;
public:
postgraduate(){};
postgraduate(string b,int c,string d):
sex(b),teacher(c,d)
{}
postgraduate(string b,string e,int f,float g):
sex(b),student(e,f,g)
{}
postgraduate &operator=(postgraduate&r)
{
teacher::operator = (r);
student::operator = (r);
this->sex = r.sex;
return *this;
}
void show_teacher()
{
cout<<"teacher age:"<<teacher::age<<" job:"<<teacher::job
<<" sex:"<<sex<<endl;
}
void student_show()
{
cout<<"student name:"<<student::name<<" age:"<<student::age
<<" score:"<<student::score<<" sex:"<<sex<<endl;
}
};
int main()
{
postgraduate s1("man",35,"teacher");
postgraduate s2("woman","zhang san",20,99.5);
s1.show_teacher ();
s2.student_show ();
return 0;
}
结果演示: