6-1 虚基类的应用-人与教师学生 (10分)

派生类定义:根据所给的基类,完成多重继承下的派生类定义

函数接口定义:
#include
#include
using namespace std;
//定义公共基类Person
class Person
{public:
Person(string nam,char s,int a)
{name=nam;sex=s;age=a;}
protected:
string name;
char sex;
int age;
};
//定义类Teacher
class Teacher:virtual public Person
{public:
Teacher(string nam,char s,int a,string t):Person(nam,s,a)
{title=t;
}
protected:
string title;
};
//定义类Student
class Student:virtual public Person
{public:
Student(string nam,char s,int a,float sco):
Person(nam,s,a),score(sco){}
protected:
float score;
};

/这里添加派生类的定义/

裁判测试程序样例:
int main( )
{Graduate grad1(“Wang-li”,‘f’,24,“assistant”,89.5,1234.5);
grad1.show( );
return 0;
}

输出样例:
在这里给出相应的输出。例如:

name:Wang-li
age:24
sex:f
score:89.5
title:assistant
wages:1234.5

class Graduate:public Teacher,public Student{
    float wages;
public: 
    Graduate(string name_,char sex_,int age_,string title_,float score_,float wages_):
    Person(name_,sex_,age_),Teacher(name_,sex_,age_,title_),Student(name_,sex_,age_,score_)
    {     
        wages=wages_;
    }
    void show(){
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"score:"<<score<<endl;
        cout<<"title:"<<title<<endl;
        cout<<"wages:"<<wages<<endl;
    }
};

2020/4/12-----By Suki

你可能感兴趣的:(c++)