class person
{
public:
person(int num1, string name1);
..
private:
int num;
string name;
};
person::person(int num1, string name1)
{
num = num1;
name = name1;
}
class student :public person
{
public:
student(int num1, string name1, int cnum1, float score1) :person(num1, name1);
...
private:
int cnum;
float score;
};
student::student(int num1, string name1, int cnum1, float score1) :person(num1, name1)
{
cnum = cnum1;
score = score1;
}
繼承類的構造函數,在類內定義,類外聲明,這種方式為什麼不可以編譯?
聲明的時候不能調用基類的構造函數,定義的時候才可以,因此,建議改為:
class person
{
public:
person(int num1, string name1);
..
private:
int num;
string name;
};
person::person(int num1, string name1)
{
num = num1;
name = name1;
}
class student :public person
{
public:
student(int num1, string name1, int cnum1, float score1) ;
...
private:
int cnum;
float score;
};
student::student(int num1, string name1, int cnum1, float score1) :person(num1, name1)
{
cnum = cnum1;
score = score1;
}