Advertisement
Cieslin

Untitled

May 21st, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class Character {
  10.  
  11. private:
  12.     char name[128];
  13.     char type[128];
  14.  
  15. protected:
  16.     Character(const char*  nameVal, const char* typeVal) {
  17.         strcpy(name, nameVal);
  18.         strcpy(type, typeVal);
  19.     }
  20.  
  21. public:
  22.     char* const getName() { //Zwraca wskaznik na łańcuch znaków char* = string
  23.         return name;
  24.     }
  25.  
  26.     char* const getType() {
  27.         return type;
  28.     }
  29.  
  30.     virtual void Draw(){ //wystarczy virutal w klasie bazowej
  31.         cout << name << " " << type << endl;
  32.     }
  33. };
  34.  
  35. class Warrior : public Character {
  36. private:
  37.     double armorLevel;
  38. public:
  39.     Warrior(const char* nameVal, const double armorLevelVal)
  40.         : Character(nameVal, "Warrior")
  41.     {
  42.         if (armorLevelVal > 0) {
  43.             armorLevel = armorLevelVal;
  44.         }
  45.     }
  46.  
  47.     double const getArmorLevel() {
  48.         return armorLevel;
  49.     }
  50.  
  51.     void setArmorLevel(const double armorLevelVal) {
  52.         armorLevel = armorLevelVal;
  53.     }
  54.  
  55.     virtual void Draw() {
  56.         Character::Draw();
  57.         cout << armorLevel << endl;
  58.     }
  59. };
  60.  
  61. class Enemy : public Character {
  62. private:
  63.     double strength;
  64.     int concurrentWarriors;
  65. public:
  66.     Enemy(const char* nameVal, const double strengthVal, const int concurrentWarriorsVal)
  67.         : Character(nameVal, "Enemy")
  68.     {
  69.         strength = strengthVal;
  70.         concurrentWarriors = concurrentWarriorsVal;
  71.     }
  72.     double const GetStrength()
  73.     {
  74.         return strength;
  75.     }
  76.  
  77.     void SetStrength(const double strengthVal)
  78.     {
  79.         strength = strengthVal;
  80.     }
  81.  
  82.     int const GetconcurrentWarriors()
  83.     {
  84.         return concurrentWarriors;
  85.     }
  86.  
  87.     virtual void Draw()
  88.     {
  89.         Character::Draw();
  90.         cout << strength << " " << concurrentWarriors << endl;
  91.     }
  92. };
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98.     const int charactersCount = 255;
  99.     Character* characters[charactersCount] = {};
  100.  
  101.     char typeTMP[128];
  102.     char nameTMP[128];
  103.     double valueWarrior;
  104.     double valueEnemy;
  105.     int valueEnemy_2;
  106.  
  107.    
  108.     ifstream file("kubusblant.txt");
  109.     if (!(file.is_open()))
  110.     {
  111.         cout << "Blad otwarcia pliku" << endl;
  112.     }
  113.  
  114.     int j = 0;
  115.     while (!(file.eof())) {
  116.         file >> typeTMP >> nameTMP;
  117.         if (strcmp(typeTMP, "Warrior") == 0)
  118.         {
  119.             file >> valueWarrior;
  120.             characters[j] = new Warrior(nameTMP, valueWarrior);
  121.         }
  122.  
  123.         if (strcmp(typeTMP, "Enemy") == 0)
  124.         {
  125.             file >> valueEnemy >> valueEnemy_2;
  126.             characters[j] = new Enemy(nameTMP, valueEnemy, valueEnemy_2);
  127.         }
  128.         j++;
  129.     }
  130.  
  131.     for (int i = 0; i < j; i++) {
  132.         characters[i]->Draw();
  133.     }
  134.  
  135.     for (int i = 0; i < j; i++) {
  136.         if (characters[i])
  137.             delete characters[i];
  138.     }
  139.  
  140.     system("PAUSE");
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement