Advertisement
szmelu

zaj2

Nov 7th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <cstdlib>
  5. using namespace std;
  6. class Ekwipunek {
  7. string nazwa="miecz";
  8. int stat;
  9. public:
  10. Ekwipunek();
  11. int get_stat();
  12. void wypisz();
  13. void set_stat(int);
  14. };
  15. class Bohater {
  16. static int licznik;
  17. int iid = 0;
  18. const int limit = 5;
  19. int ilosc_elementow;
  20. Ekwipunek *ekwipunek;
  21. public:
  22. Bohater(int);
  23. Bohater(int, Ekwipunek*);
  24. Bohater(const Bohater&);
  25. Bohater();
  26. ~Bohater();
  27. void wypisz();
  28. void zmienStat(int);
  29. static int getLicznik()
  30. {
  31. return licznik;
  32. }
  33.  
  34. };
  35. int Bohater::licznik = 0;
  36. Ekwipunek::Ekwipunek()
  37. {
  38. stat = rand() % 46 + 5;
  39. }
  40. int Ekwipunek::get_stat()
  41. {
  42. return stat;
  43. }
  44.  
  45.  
  46. void Ekwipunek::wypisz()
  47. {
  48. cout << "Nazwa: " <<nazwa<< endl;
  49. cout << "Sila: " << stat << endl;
  50.  
  51. }
  52. void Ekwipunek::set_stat(int nowaStat)
  53. {
  54. stat = nowaStat;
  55. }
  56. Bohater::Bohater()
  57. {
  58. iid = ++licznik;
  59. ekwipunek = new Ekwipunek[1];
  60. ilosc_elementow = 1;
  61. }
  62. Bohater::Bohater(int ilosc):limit(ilosc)
  63. {
  64. iid = ++licznik;
  65. ilosc_elementow = ilosc;
  66. ekwipunek = new Ekwipunek[ilosc];
  67. }
  68. Bohater::Bohater(int ilosc, Ekwipunek* nEkwipunek):limit(ilosc)
  69. {
  70. iid = ++licznik;
  71. ilosc_elementow = ilosc;
  72. ekwipunek = nEkwipunek;
  73. }
  74. Bohater::Bohater(const Bohater & kBohater)
  75. {
  76. iid = kBohater.iid;
  77. licznik++;
  78. ilosc_elementow = kBohater.ilosc_elementow;
  79. Ekwipunek* kEkwipunek = kBohater.ekwipunek;
  80. ekwipunek = new Ekwipunek[ilosc_elementow];
  81. for(int i = 0; i < ilosc_elementow; i++)
  82. {
  83. ekwipunek[i].set_stat(kEkwipunek[i].get_stat());
  84. }
  85.  
  86. }
  87. void Bohater::zmienStat(int nowaStat)
  88. {
  89. ekwipunek->set_stat(nowaStat);
  90. }
  91. void Bohater::wypisz()
  92. {
  93. cout << "IID: " << iid << endl;
  94. cout << "ilosc przedmiotow: " << ilosc_elementow << ": " << endl;
  95. for (int i = 0; i < ilosc_elementow; i++)
  96. {
  97. ekwipunek[i].wypisz();
  98. }
  99.  
  100.  
  101. }
  102. Bohater::~Bohater()
  103. {
  104. --licznik;
  105. delete[] ekwipunek;
  106. }
  107.  
  108. int main()
  109. {
  110. srand(time(NULL));
  111. Bohater A;
  112. Bohater B(7);
  113. Bohater C(10, new Ekwipunek[10]);
  114. Bohater D(A);
  115. A.wypisz();
  116. B.wypisz();
  117. D.wypisz();
  118. D.zmienStat(7);
  119. A.wypisz();
  120. D.wypisz();
  121. A.~Bohater();
  122. D.~Bohater();
  123. B.~Bohater();
  124. C.~Bohater();
  125. Bohater** sto = new Bohater*[100];
  126. for (int i = 0; i < 100; i++)
  127. {
  128. sto[i] = new Bohater(100);
  129. }
  130.  
  131. for (int i = 0; i < 100; i++)
  132. {
  133. delete sto[i];
  134. }
  135.  
  136.  
  137. cout<<Bohater::getLicznik()<<endl;
  138. system("pause");
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement