Advertisement
kamiakze

cpp02

Mar 27th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. // Abstract class ASpell
  7. class ASpell {
  8. protected:
  9. std::string name;
  10. std::string effects;
  11.  
  12. public:
  13. ASpell(const std::string& name, const std::string& effects)
  14. : name(name), effects(effects) {}
  15.  
  16. virtual ~ASpell() {}
  17.  
  18. const std::string& getName() const {
  19. return name;
  20. }
  21.  
  22. const std::string& getEffects() const {
  23. return effects;
  24. }
  25.  
  26. virtual ASpell* clone() const = 0;
  27.  
  28. void launch(const ATarget& target) const {
  29. target.getHitBySpell(*this);
  30. }
  31. };
  32.  
  33. // Concrete class Fwoosh derived from ASpell
  34. class Fwoosh : public ASpell {
  35. public:
  36. Fwoosh() : ASpell("Fwoosh", "fwooshed") {}
  37.  
  38. virtual Fwoosh* clone() const {
  39. return new Fwoosh(*this);
  40. }
  41. };
  42.  
  43. // Abstract class ATarget
  44. class ATarget {
  45. protected:
  46. std::string type;
  47.  
  48. public:
  49. ATarget(const std::string& type) : type(type) {}
  50.  
  51. virtual ~ATarget() {}
  52.  
  53. const std::string& getType() const {
  54. return type;
  55. }
  56.  
  57. virtual ATarget* clone() const = 0;
  58.  
  59. void getHitBySpell(const ASpell& spell) const {
  60. std::cout << type << " has been " << spell.getEffects() << "!" << std::endl;
  61. }
  62. };
  63.  
  64. // Concrete class Dummy derived from ATarget
  65. class Dummy : public ATarget {
  66. public:
  67. Dummy() : ATarget("Target Practice Dummy") {}
  68.  
  69. virtual Dummy* clone() const {
  70. return new Dummy(*this);
  71. }
  72. };
  73.  
  74. // Warlock class
  75. class Warlock {
  76. private:
  77. std::string name;
  78. std::string title;
  79. std::vector<ASpell*> spells;
  80.  
  81. public:
  82. Warlock(const std::string& name, const std::string& title)
  83. : name(name), title(title) {}
  84.  
  85. virtual ~Warlock() {
  86. for (auto spell : spells) {
  87. delete spell;
  88. }
  89. }
  90.  
  91. const std::string& getName() const {
  92. return name;
  93. }
  94.  
  95. const std::string& getTitle() const {
  96. return title;
  97. }
  98.  
  99. void setTitle(const std::string& title) {
  100. this->title = title;
  101. }
  102.  
  103. void introduce() const {
  104. std::cout << name << ": I am " << name << ", " << title << "!" << std::endl;
  105. }
  106.  
  107. void learnSpell(ASpell* spell) {
  108. // Check if the Warlock already knows this spell
  109. if (std::find(spells.begin(), spells.end(), spell) == spells.end()) {
  110. spells.push_back(spell);
  111. }
  112. }
  113.  
  114. void forgetSpell(const std::string& spellName) {
  115. for (auto it = spells.begin(); it != spells.end(); ++it) {
  116. if ((*it)->getName() == spellName) {
  117. delete *it;
  118. spells.erase(it);
  119. break;
  120. }
  121. }
  122. }
  123.  
  124. void launchSpell(const std::string& spellName, ATarget& target) const {
  125. for (auto spell : spells) {
  126. if (spell->getName() == spellName) {
  127. spell->launch(target);
  128. break;
  129. }
  130. }
  131. }
  132. };
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement