class Player
{
protected:
string type;
int rank;
public:
virtual void printType()
{
cout<<"Calling Class Player, type is: general Player"<<endl;
}
};
//class FootballPlayer: Derived from Player
class FootballPlayer: public Player
{
protected:
public:
virtual void printRank()
{
cout<<"Calling Class FootballPlayer, Rank is: Football Player rank"<<endl;
}
void printType()
{
cout<<"Calling Class FootballPlayer, type is: Football Player"<<endl;
}
};
class MaleFootballPlayer: public FootballPlayer
{
public:
void printType()
{
cout<<"Calling Class MaleFootballPlayer, type is: Male Football Player"<<endl;
}
void printRank()
{
cout<<"Calling Class MaleFootballPlayer, Rank is: Male Player rank"<<endl;
}
};
//class CricketPlayer: Derived from Player
class CricketPlayer: public Player
{
protected:
public:
void printType()
{
cout<<"Calling Class CricketPlayer, type is: Cricket Player"<<endl;
}
};
int main(int argc, const char * argv[])
{
FootballPlayer fbplayer;
CricketPlayer crplayer;
MaleFootballPlayer malefbplayer;
FootballPlayer *fbplayerPtr;
fbplayerPtr=&malefbplayer;
fbplayerPtr->printType();
return 0;
}
AS I运行程序,输出我得到的是,多级继承和多态
调用类MaleFootballPlayer,类型为:男子足球运动员身体素质
我创建一个基类指针(footballplayer)并分配给派生类对象(malefootballplayer),它应该调用属于基类的函数(因为它不是虚拟的),输出应该是'调用类FootBallPlayer,类型是:足球运动员'。
希望清除我的概念。
谢谢。
'Player :: printType' _is_声明为'virtual'。 –