I'm making a game. I plan on having 4 enemy types. I already made a class for one of them.
I'm new to C++ and I always was confused about Polymorphism and how and when to use it.
I need to know how I could create a class that acts as a base for all 4 of the enemy classes.
I know that with using OOP, one of the advantages is less code is used which means it's efficient. I'm not really asking for you to code a whole class for me, just asking how I could possibly use Polymorphism in this situation. Here is 1 of the 4 enemy classes. (They all will look exactly the same without Polymorphism, the only thing that will change is the chanceOfLoss
variable).
class Punk
{
public:
Punk(Player player)
{
chanceOfLoss = 50;
this->player = player;
}
bool Fight()
{
chanceOfWin = chanceOfLoss + player.GetWeapon();
randNum = 1 + rand() % 100;
if (randNum <= chanceOfWin)
{
return true;
}
return false;
}
private:
Player player;
int chanceOfLoss;
int randNum;
int chanceOfWin;
};