继承与派生
求教高手啊,继承不是不能继承基类的构造函数吗???可是我的程序竟然继承啦???求解啊#include<iostream.h>
#include<string.h>
//基类person
class person
{
protected:
char name[10];
char sex[4];
int age;
public:
person(){}
~person(){}
void show(){}
};
//派生student类
class student:virtual public person
{
protected:
char speciality[10]; //学生的专业
int num; //学生的学号
public:
student();
~student();
void show();
};
//student类构造函数的实现
student::student()
{
cout<<"请输入学生的信息:"<<endl;
cout<<"学号:";
cin>>num;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"年龄:";
cin>>age;
cout<<"专业:";
cin>>speciality;
}
//析构函数
student::~student()
{
}
//显示函数的实现
void student::show()
{
cout<<"学生的信息如下:"<<endl;
cout<<"学号"<<" "<<"姓名"<<" "<<"性别"<<" "<<"年龄"<<" "<<"专业"<<endl;
cout<<num<<" "<<name<<" "<<sex<<" "<<age<<" "<<speciality<<endl;
}
//teacher类
class teacher:virtual public person
{
protected :
char department[20];
public:
teacher();
~teacher();
void show();
};
//teacher类构造函数的实现
teacher::teacher()
{
cout<<"请输入老师的信息:"<<endl;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"年龄:";
cin>>age;
cout<<"院系:";
cin>>department;
}
//析构函数的实现
teacher::~teacher()
{
}
//显示函数的实现
void teacher::show()
{
cout<<"老师的信息如下:"<<endl;
cout<<"姓名"<<" "<<"性别"<<" "<<"年龄"<<" "<<"院系"<<endl;
cout<<name<<" "<<sex<<" "<<age<<" "<<department<<endl;
}
//派生类stuteacher
class stuteacher: public student, public teacher
{
public:
stuteacher();
~stuteacher();
void show();
};
//stuteacher类构造函数的实现
stuteacher::stuteacher()
{
cout<<"请输入在读老师的信息:"<<endl;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"年龄:";
cin>>age;
cout<<"院系:";
cin>>department;
cout<<"专业";
cin>>speciality;
}
//析构函数的实现
stuteacher::~stuteacher()
{
}
//stuteacher类显示函数的实现
void stuteacher::show()
{
cout<<"老师的信息如下:"<<endl;
cout<<"姓名"<<" "<<"性别"<<" "<<"年龄"<<" "<<"院系"<<" "<<"专业"<<endl;
cout<<name<<" "<<sex<<" "<<age<<" "<<department<<" "<<speciality<<endl;
}
void main()
{
student objs;
objs.show();
teacher objt;
objt.show();
stuteacher objst;
objst.show();
}