How can I implement this class in Polymorphism c++? -


i'm making game. plan on having 4 enemy types. made class 1 of them.

i'm new c++ , confused polymorphism , how , when use it.

i need know how create class acts base 4 of enemy classes.

i know using oop, 1 of advantages less code used means it's efficient. i'm not asking code whole class me, asking how possibly use polymorphism in situation. here 1 of 4 enemy classes. (they same without polymorphism, thing change 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; }; 

the following example of how can use polymorphism in situation. should consider if best choice, depending on design considerations.

class baseenemy base class of enemies. each enemy type inherit it, , override getchanceofloss method. enemies share same fight method

class baseenemy { public:    bool fight()    {       chanceofwin = getchanceofloss() + player.getweapon();       ...    }    virtual int getchanceofloss() = 0; }  class punk: public baseenemy { public:    virtual int getchanceofloss() { return 60; }     } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)