dimanche 24 novembre 2019

Understanding based and derived classes and how to create function calls from the base class

I have a base class called players and a derived class called attacker and midfielder. i am trying to get the goals statistics for attacker eg for each goal an attacker scores he gets a score of 3. here is some bits of my code and what i have tried


Player::Player() {

}

void Player::addGoalsScored(int g) {
    stats = stats + g;

    // IMPLEMENT ME
}

void Player::addAssists(int a) {

     stats = stats + (3 * a);//wrong
    // IMPLEMENT ME
}

int Player::getScore() const{
    return 0;

}
int Attacker::getScore() const {
    return stats;
   // 
}

I then call it like this

Team* t = new Team("Example team");
    Player* p = new Attacker("Some attacker", t);
    p->addGoalsScored();
    p->addaddAssists();
    p->getscore();

How do i get it to know that when i call get score i mean attackers score so it can multiply it by 4. or if i call the addAssists() function it should return stats multiplied by 3

Aucun commentaire:

Enregistrer un commentaire