peterzig

[PO] Polimorfizm Warrior

May 30th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. // [PO] Polimorfizm - Warrior.cpp : Defines the entry point for the console application.
  2. /////////////warrior.txt///////////////////
  3. Warrior
  4. Batman 10.2
  5. Enemy
  6. Joker 5.1 3
  7. Warrior
  8. Superman 55.3
  9. Enemy
  10. Ultra-Humanite 17.2 10
  11. Warrior
  12. Daredevil 33.7
  13. Enemy
  14. Wilson-Fisk 3.1 10
  15. Warrior
  16. Harry-Osborn 23.1 4
  17. Warrior
  18. Iron-Man 99.9 12
  19. Enemy
  20. Kingpin 45.2 1
  21. Enemy
  22. Green-Gobin 100.2 2
  23. ///////////////////////////////////////////
  24.  
  25. //#define _CRT_SECURE_NO_WARNINGS
  26.  
  27. #include "stdafx.h"
  28. #include <iostream>
  29. #include <stdlib.h>
  30. #include <string>
  31. #include <conio.h>
  32.  
  33. using namespace std;
  34.  
  35. FILE plik;
  36.  
  37. class Character
  38. {
  39. private:
  40.     char Name[128];
  41.     char Type[128];
  42. public:
  43.     Character(const char* imie, const char* typ);
  44.     char* GetName() const;
  45.     char* GetType() const;
  46.     static Character** load(char* path, int &);
  47.     virtual void Draw() = 0;
  48. };
  49.  
  50. Character::Character(const char* imie, const char* typ)
  51. {
  52.     strcpy(Name, imie);
  53.     strcpy(Type, typ);
  54. }
  55.  
  56. void Character::Draw()
  57. {
  58.     cout << "Type: " << Type << " Name: " << Name;
  59. }
  60. char* Character::GetName() const
  61. {
  62.     char* name = (char*)malloc(sizeof(char) * strlen(Name) + 1);
  63.     strcpy(name, Name);
  64.     return name;
  65. }
  66.  
  67. char* Character::GetType() const
  68. {
  69.     char* type = (char*)malloc(sizeof(char) * strlen(Type) + 1);
  70.     strcpy(type, Type);
  71.     return type;
  72. }
  73.  
  74. class Warrior : public Character
  75. {
  76. private:
  77.     double _ArmorLvl;
  78. public:
  79.     Warrior(const char* Name, double ArmorLevel) : Character(Name, "Warrior"), _ArmorLvl(ArmorLevel) {};
  80.     void SetArmorLevel(double a)
  81.     {
  82.         _ArmorLvl = a;
  83.     };
  84.     double GetArmorLevel() const
  85.     {
  86.         return _ArmorLvl;
  87.     };
  88.     virtual void Draw()
  89.     {
  90.         Character::Draw();
  91.         cout << " LevelArmor: " << _ArmorLvl << endl;
  92.     };
  93. };
  94.  
  95. class Enemy : public Character
  96. {
  97. private:
  98.     double _Strength;
  99.     int _Enemies;
  100. public:
  101.     Enemy(const char* Name, double Strength, int Enemies) : Character(Name, "Enemy"), _Strength(Strength), _Enemies(Enemies) {};
  102.     void SetStrenght(double s)
  103.     {
  104.         _Strength = s;
  105.     };
  106.     double GetStrenght() const
  107.     {
  108.         return _Strength;
  109.     };
  110.     int GetConcurrentWarriors() const
  111.     {
  112.         return _Enemies;
  113.     };
  114.     virtual void Draw()
  115.     {
  116.         Character::Draw();
  117.         cout << " Strength: " << _Strength << " ConcurrentWarriors: " << _Enemies << endl;
  118.     };
  119. };
  120.  
  121. Character** Character::load(char* path, int &k)
  122. {
  123.     int l = 0;
  124.     FILE* file;
  125.     file = fopen("warrior.txt", "r");
  126.  
  127.     Character** lista = (Character**)malloc(sizeof(Character*));
  128.  
  129.     while (!feof(file))
  130.     {
  131.         char type[128];
  132.         char name[128];
  133.         float str;
  134.         int enemies;
  135.  
  136.         fgets(type, sizeof(type), file);
  137.  
  138.         strtok(type, "\n");
  139.  
  140.         if (!strcmp(type, "Warrior"))
  141.         {
  142.             fscanf(file, "%s %f", &name, &str);
  143.             l++;
  144.             lista = (Character**)realloc(lista, sizeof(Character*) * l);
  145.             lista[l - 1] = new Warrior(name, str);
  146.         }
  147.         else if (!strcmp(type, "Enemy"))
  148.         {
  149.             fscanf(file, "%s %f %d", &name, &str, &enemies);
  150.             l++;
  151.             lista = (Character**)realloc(lista, sizeof(Character*) * l);
  152.             lista[l - 1] = new Enemy(name, str, enemies);
  153.         }
  154.     }
  155.  
  156.     fclose(file);
  157.  
  158.     k = l;
  159.  
  160.     return lista;
  161. }
  162.  
  163. int main()
  164. {
  165.     const int charactersCount = 6;
  166.     Character* characters[charactersCount] = {};
  167.  
  168.     characters[0] = new Warrior("Batman", 10.2);
  169.     characters[1] = new Enemy("Joker", 5.1, 3);
  170.     characters[2] = new Warrior("Superman", 55.3);
  171.     characters[3] = new Enemy("Ultra-Humanite", 17.2, 10);
  172.     characters[4] = new Warrior("Daredevil", 33.7);
  173.     characters[5] = new Enemy("Wilson-Fisk", 3.1, 10);
  174.  
  175.  
  176.     for (int i = 0; i < charactersCount; ++i)
  177.     {
  178.         characters[i]->Draw();
  179.     };
  180.  
  181.  
  182.     cout << characters[5]->GetName() << endl;
  183.  
  184.     for (int i = 0; i < charactersCount; ++i)
  185.     {
  186.         if (characters[i])
  187.             delete characters[i];
  188.     };
  189.  
  190.     int l;
  191.  
  192.     Character** lista = Character::load("warrior.txt", l);
  193.  
  194.     cout << "\nPo wczytaniu listy\n" << endl;
  195.  
  196.     for (int i = 0; i < l; i++)
  197.     {
  198.         lista[i]->Draw();
  199.     }
  200.  
  201.     _getch();
  202.     return 0;
  203. }
Add Comment
Please, Sign In to add comment