主题:多重继承
题目要求:
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher _Cadre(教师兼干部)。要求:
在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher _Cadre类中还包含数据成员wage(工资)。
对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
在类内声明成员函数,在类外定义成员函数。
在派生类Teacher _Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务与工资。
本人写的代码:
#include<iostream>
using namespace std;
class teacher
{
private:
char *name;
int age;
char *sex;
char *address;
char *tel;
char *title;
public: teacher(char *name="",int age = 0 , char *sex ="",
char *address = "",char *tel="",char *title ="");
void setteacher() ;
void display() const;
};
void teacher :: setteacher()
{
cout<<"请输入姓名:"<<endl;
for(int i=0;i<10;i++)
cin>>name[i];
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入性别:"<<endl;
for(int j=0;j<10;j++)
cin>>sex[j];
cout<<"请输入地址:"<<endl;
for(int k=0;k<20;k++)
cin>>address[k];
cout<<"请输入电话:"<<endl;
for(i=0;i<15;i++)
cin>>tel[i];
cout<<"请输入职称:"<<endl;
for( i=0;i<10;i++)
cin>>title[i];
}
void teacher :: display () const
{
cout<<"输出姓名:"<<endl;
for(int i=0;i<10;i++)
cout<<name[i];
cout<<"输出年龄:"<<endl;
cout<<age;
cout<<"输出性别:"<<endl;
for(int j=0;j<10;j++)
cout<<sex[j];
cout<<"输出地址:"<<endl;
for(int k=0;k<20;k++)
cout<<address[k];
cout<<"输出电话:"<<endl;
for(i=0;i<15;i++)
cout<<tel[i];
cout<<"输出职称:"<<endl;
for( i=0;i<10;i++)
cout<<title[i];
}
teacher :: teacher(char *name,int age , char *sex,
char *address ,char *tel,char *title )
{
name = "tom";
age = 20;
sex = "man";
address = "china";
tel = "12345678912";
title ="teacher";
}
class cadre
{
private:
char *name;
int age;
char *sex;
char *address;
char *tel;
char *post;
public:
cadre(char *name ="",int age = 0 , char *sex = "",
char *address ="",char *tel= "",char *post ="");
void setcadre() ;
void getcadre() const;
};
cadre:: cadre(char *name ,int age , char *sex ,
char *address ,char *tel ,char *post )
{
name ="lucy";
age = 21;
sex = "woman";
address="beijing";
tel ="12345678913";
post ="teachers";
}
void cadre :: setcadre()
{
cout<<"请输入姓名:"<<endl;
for(int i=0;i<10;i++)
cin>>name[i];
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入性别:"<<endl;
for(int j=0;j<10;j++)
cin>>sex[j];
cout<<"请输入地址:"<<endl;
for(int k=0;k<20;k++)
cin>>address[k];
cout<<"请输入电话:"<<endl;
for( i=0;i<15;i++)
cin>>tel[i];
cout<<"请输入职务:"<<endl;
for( i=0;i<10;i++)
cin>>post[i];
}
void cadre :: getcadre() const
{
cout<<"post = "<<endl;
for(int j=0;j<10;j++)
cout<<post[j]<<endl;
}
class teacher_cadre : public teacher , public cadre
{
private:
int wage;
public:
teacher_cadre(char *name ,int age , char *sex,
char *address ,char *tel,char *title,char *post,int wage);
void setteacher_cadre() const ;
void show() const ;
};
teacher_cadre :: teacher_cadre(char *name ,int age , char *sex,
char *address ,char *tel,char *title,char *post,int wage)
{
teacher::teacher();cadre::cadre();
wage=0;
void teacher_cadre::setteacher_cadre() const
{
teacher :: setteacher();
cadre :: setcadre() ;
int wage=0;
}
void teacher_cadre :: show() const
{
teacher :: display () ;
cout<<"职务:"<<cadre :: getcadre();
cout<<"wage="<<wage<<endl;
}
int main()
{
teacher_cadre teacher_cadre1;
teacher_cadre1.setteacher_cadre();
teacher_cadre1.show();
return 0;
}
但有错误,麻烦高手指点:
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher _Cadre(教师兼干部)。要求:
在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher _Cadre类中还包含数据成员wage(工资)。
对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
在类内声明成员函数,在类外定义成员函数。
在派生类Teacher _Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务与工资。
本人写的代码:
#include<iostream>
using namespace std;
class teacher
{
private:
char *name;
int age;
char *sex;
char *address;
char *tel;
char *title;
public: teacher(char *name="",int age = 0 , char *sex ="",
char *address = "",char *tel="",char *title ="");
void setteacher() ;
void display() const;
};
void teacher :: setteacher()
{
cout<<"请输入姓名:"<<endl;
for(int i=0;i<10;i++)
cin>>name[i];
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入性别:"<<endl;
for(int j=0;j<10;j++)
cin>>sex[j];
cout<<"请输入地址:"<<endl;
for(int k=0;k<20;k++)
cin>>address[k];
cout<<"请输入电话:"<<endl;
for(i=0;i<15;i++)
cin>>tel[i];
cout<<"请输入职称:"<<endl;
for( i=0;i<10;i++)
cin>>title[i];
}
void teacher :: display () const
{
cout<<"输出姓名:"<<endl;
for(int i=0;i<10;i++)
cout<<name[i];
cout<<"输出年龄:"<<endl;
cout<<age;
cout<<"输出性别:"<<endl;
for(int j=0;j<10;j++)
cout<<sex[j];
cout<<"输出地址:"<<endl;
for(int k=0;k<20;k++)
cout<<address[k];
cout<<"输出电话:"<<endl;
for(i=0;i<15;i++)
cout<<tel[i];
cout<<"输出职称:"<<endl;
for( i=0;i<10;i++)
cout<<title[i];
}
teacher :: teacher(char *name,int age , char *sex,
char *address ,char *tel,char *title )
{
name = "tom";
age = 20;
sex = "man";
address = "china";
tel = "12345678912";
title ="teacher";
}
class cadre
{
private:
char *name;
int age;
char *sex;
char *address;
char *tel;
char *post;
public:
cadre(char *name ="",int age = 0 , char *sex = "",
char *address ="",char *tel= "",char *post ="");
void setcadre() ;
void getcadre() const;
};
cadre:: cadre(char *name ,int age , char *sex ,
char *address ,char *tel ,char *post )
{
name ="lucy";
age = 21;
sex = "woman";
address="beijing";
tel ="12345678913";
post ="teachers";
}
void cadre :: setcadre()
{
cout<<"请输入姓名:"<<endl;
for(int i=0;i<10;i++)
cin>>name[i];
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入性别:"<<endl;
for(int j=0;j<10;j++)
cin>>sex[j];
cout<<"请输入地址:"<<endl;
for(int k=0;k<20;k++)
cin>>address[k];
cout<<"请输入电话:"<<endl;
for( i=0;i<15;i++)
cin>>tel[i];
cout<<"请输入职务:"<<endl;
for( i=0;i<10;i++)
cin>>post[i];
}
void cadre :: getcadre() const
{
cout<<"post = "<<endl;
for(int j=0;j<10;j++)
cout<<post[j]<<endl;
}
class teacher_cadre : public teacher , public cadre
{
private:
int wage;
public:
teacher_cadre(char *name ,int age , char *sex,
char *address ,char *tel,char *title,char *post,int wage);
void setteacher_cadre() const ;
void show() const ;
};
teacher_cadre :: teacher_cadre(char *name ,int age , char *sex,
char *address ,char *tel,char *title,char *post,int wage)
{
teacher::teacher();cadre::cadre();
wage=0;
void teacher_cadre::setteacher_cadre() const
{
teacher :: setteacher();
cadre :: setcadre() ;
int wage=0;
}
void teacher_cadre :: show() const
{
teacher :: display () ;
cout<<"职务:"<<cadre :: getcadre();
cout<<"wage="<<wage<<endl;
}
int main()
{
teacher_cadre teacher_cadre1;
teacher_cadre1.setteacher_cadre();
teacher_cadre1.show();
return 0;
}
但有错误,麻烦高手指点: