请教 继承的问题
想问一下,为什么多态继承必须要求有默认构造函数啊?#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class person
{
public:
person()
{
cout<<endl<< "person construct!" <<endl;
}
virtual void display() = 0;
protected:
int id;
string name;
};
class student: virtual public person
{
public:
student(){} //为什么一定要有个默认构造函数啊?否这就报错...?
student(int pid, string pname, int pnum, float pscore)
{
cout<<endl<< "student constructor";
id = pid;
name = pname;
num = pnum;
score = pscore;
}
void display()
{
cout<<endl<<"ID: "<<id;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Num: "<<num;
cout<<endl<<"score: "<<score;
}
protected:
int num;
float score;
};
class teacher: virtual public person
{
public:
teacher(){} //还有这里?
teacher(int pid, string pname, string pjob, string pdep)
{
cout<<endl<< "teach constructor";
id = pid;
name = pname;
job = pjob;
dep = pdep;
}
void display()
{
cout<<endl<<"ID: "<<id;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Job: "<<job;
cout<<endl<<"Department: "<<dep;
}
protected:
string job;
string dep;
};
class graduate_student: virtual public student,virtual public teacher
{
public:
graduate_student(int pid, string pname, int pnum, string pjob)
{
cout<<endl<<"graduate construct";
id = pid;
name = pname;
num = pnum;
job = pjob;
}
void display()
{
cout<<endl<<"ID: "<<id;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Num: "<<num;
cout<<endl<<"Job: "<<job;
}
};
int main()
{
student s1(2011201,"jack",1,100);
s1.display();
teacher t1(1022,"john","teacher","school");
t1.display();
graduate_student gs1(13561,"xue",23,"techer student");
gs1.display();
getch();
return 0;
}
[ 本帖最后由 飞扬冲天 于 2012-4-25 21:36 编辑 ]