Advertisement
Guest User

main

a guest
Jun 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<conio.h>
  4. #include<fstream>
  5.  
  6. using namespace std;
  7.  
  8. class Apartament
  9. {
  10. protected:
  11. const unsigned int id;
  12. string adresa;
  13. char proprietar[50];
  14. double pret;
  15. unsigned int numar_camera;
  16. float* suprafate_camere;
  17. static int nr_total;
  18.  
  19. public:
  20. //constructor fara parametri
  21. Apartament() :id(-1)
  22. {
  23. this->adresa = "Necunoscuta";
  24. strcpy(this->proprietar, "Anonim");
  25. this->pret = 0;
  26. this->numar_camera = 0;
  27. this->suprafate_camere = NULL;
  28. }
  29.  
  30. //constructor cu toti parametri
  31. Apartament(unsigned int v_id, string v_adresa, char v_proprietar[50],
  32. double v_pret, unsigned int v_numar_camera, float *v_suprafate_camera)
  33. :id(nr_total++)
  34. {
  35. if (v_adresa.length() > 0)
  36. {
  37. this->adresa = v_adresa;
  38. }
  39. else {
  40. this->adresa = "Necunoscuta";
  41. }
  42.  
  43. if (v_proprietar != NULL)
  44. {
  45. strcpy(this->proprietar, v_proprietar);
  46. }
  47. else {
  48. strcpy(this->proprietar, "Anonim");
  49. }
  50.  
  51. this->pret = v_pret;
  52. this->numar_camera = v_numar_camera;
  53. if (v_suprafate_camera != NULL)
  54. {
  55. this->suprafate_camere = new float[this->numar_camera];
  56. for (int i = 0; i < this->numar_camera; i++)
  57. {
  58. this->suprafate_camere[i] = v_suprafate_camera[i];
  59. }
  60. }
  61. }
  62.  
  63. //constructor de copiere
  64. Apartament(const Apartament& copie) : id(copie.nr_total++)
  65. {
  66. if (copie.adresa.length() > 0)
  67. {
  68. this->adresa = copie.adresa;
  69. }
  70. else {
  71. this->adresa = "Necunoscuta";
  72. }
  73.  
  74. if (copie.proprietar != NULL)
  75. {
  76. strcpy(this->proprietar, copie.proprietar);
  77. }
  78. else {
  79. strcpy(this->proprietar, "Anonim");
  80. }
  81.  
  82. this->pret = copie.pret;
  83. this->numar_camera = copie.numar_camera;
  84. if (copie.suprafate_camere != NULL)
  85. {
  86. this->suprafate_camere = new float[this->numar_camera];
  87. for (int i = 0; i < this->numar_camera; i++)
  88. {
  89. this->suprafate_camere[i] = copie.suprafate_camere[i];
  90. }
  91. }
  92. }
  93.  
  94. //destructorul
  95. ~Apartament()
  96. {
  97. if (this->suprafate_camere != NULL && this->numar_camera > 0)
  98. {
  99. delete[]this->suprafate_camere;
  100. }
  101. }
  102.  
  103. friend ostream& operator<<(ostream & out, Apartament a)
  104. {
  105. out << "Detalii despre aparatement" << endl;
  106. out << "Id-ul apartamentului este:" << a.id << endl;
  107. out << "Adresa la care se afla este:" << a.adresa <<endl;
  108. out << "Numele proprietarului este:" << a.proprietar << endl;
  109. out << "Pretul apartaentului este:" << a.pret << endl;
  110. out << "Apratamentul are un numar de" << a.numar_camera << "camere." << endl;
  111. if (a.numar_camera > 0 && a.suprafate_camere != NULL)
  112. {
  113. for (int i = 0; i < a.numar_camera; i++)
  114. {
  115. out << "Suprafata camerei" << i + 1 << "este de" << a.suprafate_camere << "metri patrati" << endl;
  116. }
  117. }
  118.  
  119. return out;
  120. }
  121.  
  122. //Setteri
  123. void setAdresa(string v_adresa) {
  124. if (v_adresa.length() > 0) {
  125. this->adresa = v_adresa;
  126. }
  127. }
  128. void setProprietar(char v_proprietar[50]) {
  129. if (v_proprietar != NULL) {
  130. strcpy(this->proprietar, v_proprietar);
  131. }
  132. else {
  133. strcpy(this->proprietar, "Anonim");
  134. }
  135. }
  136. void setPret(double v_pret) {
  137. if (v_pret > 0) {
  138. this->pret = v_pret;
  139. }
  140. }
  141. void setNrCamere(unsigned int v_numar_camere)
  142. {
  143. if (v_numar_camere > 0) {
  144. this->numar_camera = v_numar_camere;
  145. }
  146. }
  147. void setSuprafCam(float* v_suprafete_camere) {
  148. if (this->suprafate_camere != NULL) {
  149. delete[]this->suprafate_camere;
  150. }
  151. if (v_suprafete_camere != NULL) {
  152. this->suprafate_camere = new float[this->numar_camera];
  153. for (int i = 0; i < this->numar_camera; i++) {
  154. this->suprafate_camere[i] = v_suprafete_camere[i];
  155. }
  156. }
  157. }
  158. unsigned int getId() {
  159. return this->id;
  160. }
  161. string getAdresa() {
  162. return this->adresa;
  163. }
  164. char* getProprietar() {
  165. return this->proprietar;
  166.  
  167. }
  168. double getPret() {
  169. return this->pret;
  170. }
  171. unsigned int getNrCam() {
  172. return this->numar_camera;
  173. }
  174. float* getSuprafCam() {
  175. return this->suprafate_camere;
  176. }
  177. };
  178.  
  179. int main()
  180. {
  181. // Test commands
  182. Apartament apart; //default constructor no parameters
  183. apart.afisare();
  184. cout << "-----------------------------------------------------" << endl;
  185. cin >> apart;
  186. cout << "-----------------------------------------------------" << endl;
  187. cout << apart;
  188. cout << "-----------------------------------------------------" << endl;
  189. Apartament apart2(1,"My adress","Ion Paunescu",12.4,2,new float[2] { 12.5f, 15.2f});
  190. apart2.afisare();
  191. cout << "-----------------------------------------------------" << endl;
  192. // Test Setteri
  193. cout << "Setteri au fost folositi pentru obiectul 'apart2'" << endl;
  194. apart2.setAdresa("My new address");
  195. apart2.setProprietar("Popescu Ion");
  196. apart2.setNrCamere(1);
  197. apart2.setPret(15.5);
  198. apart2.setSuprafCam(new float[1] { 21.5f });
  199. cout << "-----------------------------------------------------" << endl;
  200. // Test Getteri
  201. cout << "Getteri" << endl;
  202. cout << "Id apartament : " << apart2.getId() << endl;
  203. cout << "Adresa : " << apart2.getAdresa()<<endl;
  204. cout << "Nume proprietar : "<<apart2.getProprietar()<<endl;
  205. cout << "Pret : "<<apart2.getPret()<<endl;
  206. cout << "Numar camere : "<<apart2.getNrCam()<<endl;
  207. cout << "Suprafete camere : "<<endl;
  208. float *sup = apart2.getNumePersoane();
  209. for (int i = 0; i < apart2.getSuprafCam(); i++)
  210. {
  211. cout << i + 1 << ". " << sup[i] << endl;
  212. }
  213. cout << "-----------------------------------------------------" << endl;
  214. // Test Operatori -> aici cam astea sunt testele in functie si de cerintele date de el
  215. cout << "Operator overloading" << endl;
  216. cout << "String cast operator : " << (string)apart2 << endl;
  217. cout << "() operator(nume care incep cu vocala) : " << apart2() << endl;
  218. ++apart2;
  219. cout << "++(pre) operator : " << apart2.getPret() << endl;
  220. apart2++;
  221. cout << "(post)++ operator : " << apart2.getPret() << endl;
  222. --apart2;
  223. cout << "--(pre) operator : " << apart2.getPret() << endl;
  224. apart2--;
  225. cout << "(post)-- operator : " << apart2.getPret() << endl;
  226. apart2 += apart;
  227. cout << "+= operator : " << apart2.getPret() << endl;
  228. cout << "[] operator : " << apart2[0] << endl;
  229. cout << "! operator : " << !apart2 << endl;
  230. cout << "== operator : " << (apart2 == apart) << endl;
  231. cout << "> operator : " << (apart2 > apart) << endl;
  232. cout << ">= operator : " << (apart2 >= apart) << endl;
  233. cout << "< operator : " << (apart2 < apart) << endl;
  234. cout << "<= operator : " << (apart2 <= apart) << endl;
  235. apart2 = apart2 + apart;
  236. cout << "+ operator : " << apart2.getPret() << endl;
  237. apart2 = apart2 - apart;
  238. cout << "- operator : " << apart2.getPret() << endl;
  239. cout << "-----------------------------------------------------" << endl;
  240. _getch();
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement