派生类定义:根据所给的基类,完成多重继承下的派生类定义
函数接口定义:
#include <iostream>
#include <string>
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:virtual public Person ,public Teacher,public Student
{
public:
double wages;
Graduate(string nam,char s,int a,string t,float sco,double w):Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco)
{
title=t;
score=sco;
name=nam;
sex=s;
age=a;
wages=w;
}
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;
}
};