代码实现
定义一个学生类:成员-->姓名、年龄、成绩以及相关函数
定义一个老师类:成员-->年龄、职称以及相关函数
由学生类和老师类共同派生出研究生类:成员-->性别
实例化就研究生类的对象,并输出该对象的所有信息
#include <iostream>
#include <string>
using namespace std;
class stu
{
public:
stu(){}
~stu(){}
stu(string a, int b):name(a), score(b){}
virtual void show()
{
cout<<"name:"<<name<<'\t'<<"score:"<<score<<endl;
}
protected:
string name;
int score;
};
class tea
{
public:
tea(){}
~tea(){}
tea(int c, string d):age(c), work(d){}
virtual void show()
{
cout<<"teacher's age:"<<age<<'\t'<<"teacher's work:"<<work<<endl;
}
protected:
int age;
string work;
};
class graduate:public stu, public tea
{
public:
graduate(){}
~graduate(){}
graduate(string a, int b, int c, string d, string e):stu(a,b), tea(c,d), sex(e){}
void show()
{
cout<<"name:"<<name<<'\t'<<"score:"<<score<<'\t'<<"teacher's age:"<<age<<'\t'<<"teacher's work:"<<work<<'\t'<<"sex:"<<sex<<endl;
}
private:
string sex;
};
int main()
{
graduate graduate_1("whp", 20, 30, "Chinese", "man");
stu* p1 = &graduate_1;
tea* p2 = &graduate_1;
p1->show();
p2->show();
graduate graduate_2("Wwwhpo", 20, 30, "English", "woman");
// graduate_2.stu::show();
// graduate_2.tea::show();
graduate_2.show();
cout << "Hello World!" << endl;
return 0;
}