main.cpp
-----------
lines 27-29: No reason to declare myWarrior, myAssasin, or myMage here. Use
new
instead. Since myPlayer is allocated dynamically, don't forget to
delete
myPlayer at the end of the program to prevent a memory leak.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
if (choice == '1')
{ myPlayer = new Warrior;
myPlayer->setName(name);
myPlayer->setMaxRage(100); // See comments below about rage and maxRage.
myPlayer->setRage(0);
}
else if (choice == '2')
{ myPlayer = new Assassin;
myPlayer->setName(name);
myPlayer->setMaxEnergy(100); // See comments below about energy and maxEnergy.
myPlayer->setEnergy(100);
}
else if (choice == '3')
{ myPlayer = new Mage;
myPlayer->setName(name);
}
|
Line 55: Get rid of the
goto
. gotos are evil.
player.h
---------
Line 14: If only Warriors have rage and maxRage, then rage and maxRage do not belong as members of Player.
Line 15: If only Assassins have energy and maxEnery, then enery and maxEnergy don't belong as member variables.
Line 16: If only Mage have mana, then mana and maxMana do not belong as members of Player.
Lines 45-49: getter and setters for rage and maxRage do not belong as member functions of Player.
lines 51-55: getters and setters for energy and maxEnergy don;t belong as member functions of Player.
Line 57-63: Ditto for getMana, sertMana, getMaxMana and setMaxMana.
player.cpp
----------
Line 89:107: ditto regarding getRage, setRage, getMaxRage, setMaxRage functions.
Lines 109-127: ditto regarding getEnergy, setEnergy, getMaxEnergy and setMaxEnergy functions.
Line 129-147: ditto regarding getMana/setMana, getMaxMana and setMaxMana functions.
Now you may be wondering if these functions are not members of Player, how do I access them, if I only have a pointer to a player. Good question. I would suggest adding the following accessors to your Player class:
1 2 3 4
|
bool isWarrior () const
{ return className == "Warrior"; }
// ditto for Assassin and Mage
|
Now that we can tell the derived type of the player, we can do a
static_cast
1 2 3 4 5 6
|
Warrior * warrior;
if (myPlayer->isWarrior())
{ warrior = static_cast<Warrior *> (myPlayer);
// Do warrior stuff
}
// Ditto for Assassin and Mage
|