Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class game
  6. {
  7. char* name;
  8. string arma;
  9. int str;
  10. public:
  11. int nrRunde = 0;
  12. int* puncte = NULL;
  13. static int players;
  14. const int id = 0;
  15.  
  16.  
  17. //constructor default
  18. game():id(0)
  19. {
  20. this->name = new char[7];
  21. strcpy(this->name, "Noname");
  22. arma = "Nici o arma";
  23. this->str = 0;
  24. game::players++;
  25. }
  26. //constructor 1 parametru
  27. game(const char* nume_new) :id(players)
  28. {
  29. this->name = new char[strlen(nume_new) + 1];
  30. strcpy(this->name, nume_new);
  31. arma = "Nici o arma";
  32. this->str = 0;
  33. game::players++;
  34. }
  35. //constructor cu 2 parametri
  36. game(const char* nume_new,string arma_new, int str_new) :id(players)
  37. {
  38. this->name = new char[strlen(nume_new) + 1];
  39. strcpy(this->name, nume_new);
  40. arma = arma_new;
  41. this->str = str_new;
  42. game::players++;
  43. }
  44. //constructor de copiere
  45. game(const game& t):id(t.id)
  46. {
  47. this->name = new char[strlen(t.name) + 1];
  48. strcpy(this->name, t.name);
  49. this->arma = t.arma;
  50. this->str = t.str;
  51. this->nrRunde = t.nrRunde;
  52.  
  53. if (t.puncte)
  54. {
  55. this->puncte = new int[nrRunde];
  56. for (int i = 0; i < t.nrRunde; i++)
  57. {
  58. this->puncte[i] = t.puncte[i];
  59. }
  60. }
  61. game::players++;
  62. }
  63.  
  64. //setter name
  65. void setName(const char* name)
  66. {
  67. if (this->name != NULL)
  68. {
  69. delete[] this->name;
  70. }
  71. this->name = new char[strlen(name) + 1];
  72. strcpy(this->name, name);
  73. }
  74. //getter name
  75. char* getName()
  76. {
  77. return this->name;
  78. }
  79. //setter arma
  80. void setArma(string numeArma)
  81. {
  82. if (numeArma.length() > 0)
  83. {
  84. arma = numeArma;
  85. }
  86. }
  87.  
  88. //getter arma
  89. string getArma()
  90. {
  91. return this->arma;
  92. }
  93.  
  94. //setter str
  95. void setStr(int str)
  96. {
  97. this->str = str;
  98. }
  99.  
  100. //getter str
  101. int getStr()
  102. {
  103. return this->str;
  104. }
  105.  
  106. //setter puncte
  107. void setPuncte(int nrRunde, int* puncte)
  108. {
  109. this->nrRunde = nrRunde;
  110. this-> puncte = new int[nrRunde];
  111. for (int i = 0; i < this->nrRunde; i++)
  112. {
  113. this->puncte[i] = puncte[i];
  114. }
  115. }
  116.  
  117. //display
  118. void display()
  119. {
  120. cout << endl << "____" << endl;
  121. cout << "Id Jucator: " << this->id << endl;
  122. cout << "Nume jucator: " << this->name << endl;
  123. cout << "Arma folosita: " << this->arma << endl;
  124. cout << "Putere jucator: " << this->str << endl;
  125. if (puncte != NULL)
  126. {
  127. cout << "Puncte jucator: ";
  128. for (int i = 0; i < nrRunde; i++)
  129. {
  130. cout << puncte[i] << " | ";
  131. }
  132. cout << endl;
  133. }
  134. else
  135. cout << "Jucatorul nu are puncte" << endl;
  136. cout << "____" << endl;
  137. }
  138.  
  139. ~game()
  140. {
  141. if (this->name != NULL)
  142. {
  143. delete[] this->name;
  144. }
  145. if (this->puncte != NULL)
  146. {
  147. delete[] this->puncte;
  148. }
  149. game::players--;
  150. cout << "Destructor" << endl;
  151. }
  152.  
  153. game operator=(const game& t)
  154. {
  155. if (this->name != NULL)
  156. {
  157. delete[] this->name;
  158. }
  159. if (puncte != NULL)
  160. {
  161. delete[] this->puncte;
  162. }
  163. this->name = new char[strlen(t.name) + 1];
  164. strcpy(this->name, t.name);
  165. this->arma = t.arma;
  166. this->str = t.str;
  167. this->nrRunde = t.nrRunde;
  168.  
  169. if (t.puncte)
  170. {
  171. this->puncte = new int[nrRunde];
  172. for (int i = 0; i < t.nrRunde; i++)
  173. {
  174. this->puncte[i] = t.puncte[i];
  175. }
  176. }
  177. else
  178. {
  179. nrRunde = 0;
  180. puncte = NULL;
  181. }
  182.  
  183. game::players++;
  184. return *this;
  185. }
  186.  
  187. game& operator++()
  188. {
  189. this->str++;
  190. return *this;
  191. }
  192.  
  193. game& operator++(int i)
  194. {
  195. game copie = *this;
  196. this->str++;
  197. return copie;
  198. }
  199.  
  200. game operator+(int i)
  201. {
  202. this->str += i;
  203. return *this;
  204. }
  205.  
  206.  
  207.  
  208. friend game operator+(int,game&);
  209. friend ostream& operator<<(ostream&, game&);
  210. friend istream& operator>>(istream&, game&);
  211.  
  212.  
  213.  
  214. };//final clasa
  215. int game::players = 0;
  216.  
  217. game operator+(int i,game& t)
  218. {
  219. t.str += i;
  220. return t;
  221. }
  222.  
  223. ostream& operator<<(ostream& out, game& t)
  224. {
  225. out << endl << "____" << endl;
  226. out << "Id Jucator: " << t.id << endl;
  227. out << "Nume jucator: " << t.name << endl;
  228. out << "Arma folosita: " << t.arma << endl;
  229. out << "Putere jucator: " << t.str << endl;
  230. if (t.puncte != NULL)
  231. {
  232. cout << "Puncte jucator: ";
  233. for (int i = 0; i < t.nrRunde; i++)
  234. {
  235. cout << t.puncte[i] << " | ";
  236. }
  237. cout << endl;
  238. }
  239. else
  240. out << "Jucatorul nu are puncte" << endl;
  241. out << "____" << endl;
  242. return out;
  243. }
  244.  
  245. istream& operator>>(istream& in, game& t)
  246. {
  247. char* name;
  248. cout << "Name: ";
  249. in >> name;
  250. t.setName(name);
  251.  
  252. return in;
  253.  
  254. }
  255.  
  256.  
  257. int main()
  258. {
  259. game p1;
  260. /*p1.display();
  261.  
  262. p1.setName("Ionel");
  263. p1.setArma("Cutit");
  264. p1.setStr(5);
  265.  
  266. p1.display();
  267.  
  268. cout << endl << endl;
  269. cout << p1.getName() << endl;
  270. cout << p1.getArma() << endl;
  271. cout << p1.getStr() << endl;*/
  272.  
  273. game p2("Dracula");
  274. //p2.display();
  275.  
  276. game p3("Vader");
  277. //p3.display();
  278.  
  279. game p4("Kobra", "Topor", 10);
  280. int pts[] = { 10,15,10 };
  281. int runde = 3;
  282. p4.setPuncte(runde, pts);
  283. p4.display();
  284. //++p4;
  285. //p4++;
  286. p4+2;
  287. 2 + p4;
  288. //p4.display();
  289. cout << p4;
  290.  
  291. game p5(p4);
  292. //p5.display();
  293.  
  294. game p6;
  295. p6 = p5;
  296. //p6.display();
  297.  
  298. game p7;
  299. cin>> p7;
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement