How to make difference between parent and child class?

Hi!

I recently got a task where I must create a vector class (SoccerTeam) for another class's pointers called SoccerPlayer. I also have twho children from SoccerPlayer (Goalkeeper and FieldPlayer).

I must define a function where I add a player to the team (I used a vector in SoccerTeam) with push_back. I must find out if it's a Goalkeeper or a Fieldplayer in order to tell if the player can be added to the team (one team can only have one Goalkeeper, that's the rule in this case).

However, I am stuck with making a difference between the two children classes since I use a pointer from the parent (SoccerPlayer*). I can't edit the parent, so I can't define a simple virtual function in it to make difference between the two children. Could you help me with it, please?

Thanks is advance!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

class SoccerPlayer{
    const std::string name;
    short speed;
    short stamina;

  public:
    SoccerPlayer(const std::string& name, short speed, short stamina);
    virtual ~SoccerPlayer() = default;
    const std::string& getName() const { return name; }
    short getSpeed() const { return speed; };
    short getStamina() const { return stamina; };
    virtual short getOverall() const = 0;

};

SoccerPlayer::SoccerPlayer(const std::string& name, short speed, short stamina) : name(name), speed(speed), stamina(stamina) {}

ostream& operator <<(ostream& out, const SoccerPlayer& pointer){
  out << pointer.getName() << " (" << pointer.getOverall() << ')';
  return out;
} 

class FieldPlayer : public SoccerPlayer{
  private:
    short shooting;
    short passing;
    short tackling;

  public:
    FieldPlayer(const std::string& name, short speed, short stamina, short shooting, short passing, short tackling) : SoccerPlayer(name, speed, stamina), shooting(shooting), passing(passing) , tackling(tackling) {}

    short getShooting() const { return shooting; }
    short getPassing() const { return passing; }
    short getTackling() const { return tackling; }
    short getOverall() const {
      
      short er = getSpeed() + getStamina() + shooting + passing + tackling;
      er = er / 5;

      return er; 
     }

    ~FieldPlayer(){};
};

class Goalkeeper : public SoccerPlayer{
  private:
    short reflexes;
    short agility;

  public:
    Goalkeeper(const std::string& name, short speed, short stamina, short reflexes, short agility) : SoccerPlayer(name, speed, stamina), reflexes(reflexes), agility(agility) {}

    short getReflexes() const { return reflexes; }
    short getAgility() const { return agility; }
    short getOverall() const { 
      short er = getSpeed() + getStamina() + reflexes + agility;
        er = er / 4;
      return er; }

    ~Goalkeeper(){};
};

class SoccerTeam{
  private:
    std::vector<const SoccerPlayer*> team;

  public:
    SoccerTeam() = default;
    ~SoccerTeam() = default;

    void addPlayer(const SoccerPlayer* new_player){ 

      if(team.empty() == true){
        team.push_back(new_player);

      }else{
        if(//if the object-type of the player is Fieldplayer){
          team.push_back(new_player);

        }else if(//if the object-type of the player is Goalkeeper){
          if(std::find(team.begin(), team.end(), new_player) != team.end()){
		return;	//there is already a Goalkeeper in class, so nothing happens
	  }else{
	  	team.push_back(new_player); //there isn't any Goalkeeper in class, add the new_player
	  }
        }
      }
    }

    int playerCount(){ return team.size(); }
    
    const SoccerPlayer* operator [](int i) {
      return team[i];
    }

};


main.cpp(Also given, can't be modified)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "solution.hpp"

int main() {
  
  const FieldPlayer* fp1 = new FieldPlayer("C. Ronaldo", 90, 85, 95, 91, 76);
  const FieldPlayer* fp2 = new FieldPlayer("L. Messi", 88, 93, 75, 88, 96);
  const Goalkeeper* gk = new Goalkeeper("M. Neuer", 89, 87, 94, 95);

  const SoccerPlayer* p1 = fp1;
  const SoccerPlayer* p2 = fp2;
  const SoccerPlayer* p3 = gk;

  cout << p1->getName() << endl;
  cout << p1->getSpeed() << endl;
  cout << p1->getStamina() << endl;
  cout << fp1->getShooting() << endl;
  cout << fp1->getPassing() << endl;
  cout << fp1->getTackling() << endl;
  cout << p1->getOverall() << endl;
  cout << endl;

  cout << fp2->getName() << endl;
  cout << fp2->getSpeed() << endl;
  cout << fp2->getStamina() << endl;
  cout << fp2->getShooting() << endl;
  cout << fp2->getPassing() << endl;
  cout << fp2->getTackling() << endl;
  cout << fp2->getOverall() << endl;
  cout << endl;

  cout << gk->getName() << endl;
  cout << gk->getSpeed() << endl;
  cout << gk->getStamina() << endl;
  cout << gk->getReflexes() << endl;
  cout << gk->getAgility() << endl;
  cout << gk->getOverall() << endl;
  cout << endl;

  cout << *p1 << endl;
  cout << *p2 << endl;
  cout << *p3 << endl;

  SoccerTeam team;
  team.addPlayer(p1);

  std::cout << "Members of team:\n;
  for(size_t i = 0; i < team.size(); i++){
	std::cout << team[i]->getName() << '\n';
  }

  delete p1;
  delete fp2;
  delete gk;

  return 0;
} 
Last edited on
Since SoccerPlayer is a polymorphic type, we can use dynamic_cast.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class SoccerTeam {

  private:
    std::vector<const SoccerPlayer*> team;

    bool has_goal_keeper = false ; // *** added

    // *** added *** returns true if the player is a Goalkeeper
    static bool is_goal_keeper( const SoccerPlayer* player ) noexcept {

        return dynamic_cast<const Goalkeeper*>(player) ;
    }

  public:
    SoccerTeam() = default;
    ~SoccerTeam() = default;

    void addPlayer( const SoccerPlayer* new_player ) {

         // if the player is a goal keeper
         if( is_goal_keeper(new_player) ) {

            if( has_goal_keeper == false ) { // and the team has no goalkeeper as yet

                team.push_back(new_player) ; // add the goal keeper
                has_goal_keeper = true ; // we have a goal keeper now
            }
            // else do nothing
         }

         else team.push_back(new_player); // not a goal keeper; add the player
    }
    
    // ...
};
It worked perfectly, just the help I needed for my code! Thank you very-very much!!
Topic archived. No new replies allowed.