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];
}
};
|