2017-07-27 104 views
0

我有一個基類,然後是它的兩個派生類。我正在使用基類的指針來存儲Vector中的派生類對象。現在我要打印存儲在矢量C++多態性在一個向量中存儲基類指針

我怎麼能檢索的矢量對象中的數據。

我怎樣才能確保到派生類檢索的對象所屬。

class CricketPlayer:public SlumsMember 
{ 
    protected: 
     int runsScored; 
     int wicketsTaken; 
     int catchesTaken; 
    public: 
     CricketPlayer(int rNo,string n,double theGpa,char typ,int rScore,int theWicketTaken,int theCatchTaken); 
     int getRunsScored(); 
     int getWicketsTaken(); 
     int getCatchesTaken(); 

}; 

    CricketPlayer::CricketPlayer(int rNo,string n,double theGpa,char typ,int rScore,int theWicketTaken,int theCatchTaken): 
        SlumsMember(rNo,n,theGpa,typ) 
        { 
         runsScored=rScore; 
         wicketsTaken=theWicketTaken; 
         catchesTaken=theCatchTaken; 
        } 



    int CricketPlayer::getRunsScored() 
    { 
     return (runsScored); 

    } 
    int CricketPlayer::getWicketsTaken() 
    { 
     return (wicketsTaken); 
    } 
    int CricketPlayer::getCatchesTaken() 
    { 
     return(catchesTaken); 
    } 

class FootballPlayer:public SlumsMember 
{ 
protected: 
    int goalsScored; 
    int assists; 
    int interceptions; 
public: 

    FootballPlayer(int rNo,string n,double theGpa,char typ,int theGoalsScored,int theAssists,int theInterceptions); 
    int getGoalsScored(); 
    int getAssists(); 
    int getInterceptions(); 

}; 

    FootballPlayer::FootballPlayer(int rNo,string n,double theGpa,char typ,int theGoalsScored,int theAssists,int theInterceptions): 
        SlumsMember(rNo,n,theGpa,typ) 
        { 
          goalsScored=theGoalsScored; 
          assists=theAssists; 
          interceptions=theInterceptions; 
        } 

    int FootballPlayer::getGoalsScored() 
    { 
      return(goalsScored); 

    } 
    int FootballPlayer::getAssists() 
    { 
     return(assists); 

    } 
    int FootballPlayer::getInterceptions() 
    { 
      return(interceptions); 
    } 

這裏我使用向量來存儲載體中的對象。

INT主要(){

vector<SlumsMember> members; 

SlumsMember *slumsMember; 

        slumsMember=new FootballPlayer(rNo,name,gpa,ch,a,b,c); 


        slumsMember=new CricketPlayer(rNo,name,gpa,ch,a,b,c); 





      members.push_back(*slumsMember); 

SlumsMember *mbr; 
for(int i=0;i<members.size();i++) 
{ 
    mbr=members[i]; 

    //How to make sure to which base class an object retrieved belongs to and how to access it. 

}  







return 0; 
} 
+0

我已經試過甚至通過會員訪問[我] - > getType()以及但沒有任何發生 – ProgrammingNinja

回答

1

我清理你的代碼有點...

class SlumsMember 
{ 

試試這個...使基類中的getWicketsTaken功能虛擬

public: 
    virtual int getWicketsTaken(); 

    int rollNumber; 
    string name; 
    double gpa; 
    char type; 
}; 



class CricketPlayer:public SlumsMember 
{ 
public: 

覆蓋可以幫助防止類型過載

int getWicketsTaken() override; 

    int runsScored; 
    int wicketsTaken; 
    int catchesTaken; 
}; 



class FootballPlayer: public SlumsMember 
{ 
Then you can override the getWicketsTaken in this class too 

int getWicketsTaken() override; 

public: 
    int goalsScored; 
    int assists; 
    int interceptions; 
}; 

int main() { 
    vector<SlumsMember*> members; 

請注意,下一行,你應該,如果你給它的價值不是一個類型slumsmember的收到一個編譯器錯誤。

members.push_back(new FootballPlayer(rNo,name,gpa,ch,a,b,c)); 
    members.push_back(new CricketPlayer(rNo,name,gpa,ch,a,b,c)); 

    SlumsMember *mbr; 
    for(int i=0;i<members.size();i++) 
    { 
     mbr=members[i]; 
     //How to make sure to which base class an object retrieved belongs to and how to access it. 
     int rollNumber = mbr->rollNumber; 
     double gpa = mbr->gpa; 

等等,不要忘記在某個時刻釋放在年底你的對象,這樣你就不會得到內存泄漏

 } 

    return 0; 
} 
+0

我已經有getter和setter只是沒有發佈它們。 – ProgrammingNinja

+0

但你讀過答案的其餘部分嗎?隨意提問,如果我的回答有什麼不太明白 –

+0

請注意,我改變 矢量成員; 至 vector 成員; –