Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. // ApkPolimorfizm.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. class Character
  9. {
  10. private:
  11. char name[128];
  12. char type[128];
  13. public:
  14. const char *getType()
  15. {
  16. return type;
  17. }
  18. const char *getName()
  19. {
  20. return name;
  21. }
  22. virtual void draw() = 0;
  23. protected:
  24. Character(const char *nameVal, const char *typeVal)
  25. {
  26. for (int i = 0; i < strlen(nameVal)+1; i++)
  27. {
  28. name[i] = nameVal[i];
  29. }
  30. for (int i = 0; i < strlen(typeVal)+1; i++)
  31. {
  32. type[i] = typeVal[i];
  33. }
  34. }
  35. };
  36.  
  37. class Warrior : public Character
  38. {
  39. private:
  40. float armor;
  41. public:
  42. Warrior(const char *nameVal, float armorVal) : Character(nameVal ,"Warrior"), armor(armorVal) {}
  43. void setArmorLevel(float armorVal)
  44. {
  45. armor = armorVal;
  46. }
  47. float getArmorLevel()
  48. {
  49. return armor;
  50. }
  51. void draw()
  52. {
  53. cout << "Imie: " << getName() << endl;
  54. cout << "Typ postaci: " << getType() << endl;
  55. cout << "Ilosc pancerza: " << getArmorLevel() << endl;
  56. }
  57. };
  58.  
  59. class Enemy : public Character
  60. {
  61. private:
  62. float strenght;
  63. int concurrentWarriors;
  64. public:
  65. Enemy(const char *nameVal, const float strenghtVal, const int concurrentWarriorsVal) : Character(nameVal, "Enemy"), strenght(strenghtVal), concurrentWarriors(concurrentWarriorsVal){}
  66. float getStrenght()
  67. {
  68. return strenght;
  69. }
  70. void setStrenght(float strenghtVal)
  71. {
  72. strenght = strenghtVal;
  73. }
  74. int getConcurrentWarriors()
  75. {
  76. return concurrentWarriors;
  77. }
  78. void draw()
  79. {
  80. cout << "Imie: " << getName() << endl;
  81. cout << "Typ postaci: " << getType() << endl;
  82. cout << "Sila: " << getStrenght() << endl;
  83. cout << "Walczacy wojownicy: " << getConcurrentWarriors() << endl;
  84. }
  85. };
  86.  
  87.  
  88. int main()
  89. {
  90. const int characterCount = 6;
  91. Character *characters[characterCount] = {};
  92.  
  93. characters[0] = new Warrior("Batman", 10.2);
  94. characters[1] = new Enemy("Joker", 5.1, 3);
  95. characters[2] = new Warrior("Superman", 55.3);
  96. characters[3] = new Enemy("Ultra-Humanite", 17.2, 10);
  97. characters[4] = new Warrior("Daredevil", 33.7);
  98. characters[5] = new Enemy("Wilson Fisk", 3.1, 10);
  99.  
  100. for (int i = 0; i < characterCount; i++)
  101. {
  102. characters[i]->draw();
  103. }
  104. for (int i = 0; i < characterCount; i++)
  105. {
  106. if (characters[i])
  107. delete characters[i];
  108. }
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement