Without design pattern - pure polimorphism
unknown
ruby
3 years ago
2.2 kB
0
Indexable
Never
#include <iostream> using namespace std; enum class PrimaryWeapon { Sword, Axe, Bow, MagicBook }; enum class SecondaryWeapon { ShortSword, Shield, Arrows, None }; enum class Tools { Lockpick, Bandages, Adrenaline, ManaPotion }; struct StartingEquipment { PrimaryWeapon primaryWeapon; SecondaryWeapon secondaryWeapon; Tools tools; }; class HeroClass { public: virtual void create() = 0; void setStrength(int a_strength) { strength = a_strength; } int getStrength() const { return strength; } void setDefence(int a_defence) { defence = a_defence; } int getDefence() const { return defence; } void setSpeed(int a_speed) { speed = a_speed; } int getSpeed() const { return speed; } void setIntelligence(int a_intelligence) { intelligence = a_intelligence; } int getIntelligence() const { return intelligence; } void setStartingEquipment(StartingEquipment a_startingEquipment) { startingEquipment = a_startingEquipment; } StartingEquipment getStartingEquipment() const { return startingEquipment; }; private: int strength; int defence; int speed; int intelligence; StartingEquipment startingEquipment; }; class Barbarian : public HeroClass { public: void create() { setStrength(10); setDefence(5); setSpeed(3); setIntelligence(1); setStartingEquipment(StartingEquipment{ PrimaryWeapon::Axe, SecondaryWeapon::None, Tools::ManaPotion }); } }; class Magician : public HeroClass { public: void create() { setStrength(1); setDefence(3); setSpeed(5); setIntelligence(10); setStartingEquipment(StartingEquipment{ PrimaryWeapon::MagicBook, SecondaryWeapon::None, Tools::Bandages }); } }; int main() { HeroClass* hero = new Magician(); hero->create(); cout << hero->getStrength(); /// let's make prototype, to have possibility to copy character, in case your character dies, /// and it's rougelike so you will lost all your equipment, but your stats, will be coppied. return 0; }