我正在进行继承,但我遇到了这个奇怪的错误。我做错了什么?
我认为代码在我试图实现的目标上会非常直接。目标是创建多个子类,以识别不同类别的几个不同的奥运会参与者。因此,aType变量wille在每个不同的子类中都是常量。但我仍然希望能够使用baseclass中的printAttributes函数。
class olympicAttender{
public:
string aName;
int aAge;
string aGender;
string aType;
void speak(){
cout << aName <<" speaks!" << endl;
}
void printAttributes(){
cout << "Name: " << aName << endl;
cout << "Age: " << aAge << endl;
cout << "Gender: " << aGender << endl;
cout << "Type: " << aType << endl;
cout << "----------------------------" << endl;
}
};
class sprinter : public olympicAttender{
public:
aType = "Sprinter";
void run(){
cout << aName <<" runs!" << endl;
}
};
int main(){
sprinter Sprinter1;
Sprinter1.aAge = 31;
Sprinter1.aGender = "Male";
Sprinter1.aName = "Usain Bolt";
Sprinter1.printAttributes();
}