给个定义的形式和例子:
定义形式:
派生类构造函数名(总参数列表):基类1构造函数(参数列表),基类2构造函数(参数列表),....基类n构造函数(参数列表)
{ 派生类中新增成员初始化语句}
例子:
class teacher //基类1
{
public:
teacher(string nam,int a,string t) //构造函数
{
name=nam;
age=a;
title=t;
}
void display()
{
cout<<"name:"<
cout<<"age:"< cout<<"title:"< }
protected:
string name;
int age;
string title;
};
class student //基类2
{
public:
student(char nam[],char s,float sco) //基类2的构造函数
{
strcpy(name1,nam);
sex=s;
score=sco;
}
void display1()
{
cout<<"name:"< cout<<"sex:"< cout<<"score:"< }
protected:
string name1;
char sex;
float score;
};
class graduate:public teacher,public student //以公有的方式分别从teacher 和student继承
{
public:
graduate(string nam,int a,char s,string t,float sco,float w):
teacher(nam,a,t),student(nam,s,sco),wage(w)
{
}
void show()
{
cout<<"name:"< cout<<"age:"< cout<<"sex:"< cout<<"score:"< cout<<"title:"< cout<<"wage:"< }
private:
float wage;
};
阅读(1187) | 评论(0) | 转发(0) |