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;
}
내가 프로그램을 실행, 나는이 얻을 출력,다단계 상속 및 다형성
클래스 MaleFootballPlayer를 호출, 유형 : 남자 축구 선수
나는 기본 클래스 포인터 (footballplayer)을 생성하고 파생 클래스 객체 (malefootballplayer)에 할당하면 기본 클래스에 속하는 함수를 호출해야하며 (가상으로 만들어지지 않으므로) 출력은 'FootBallPlayer, type is : Football Player'클래스를 호출해야합니다.
내 개념을 지우고 싶습니다.
감사합니다.
'Player :: printType' _is_'virtual'을 선언했습니다. –