Advertisement
LDG2875

Untitled

Apr 19th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. /// Tema 6
  2. #include <iostream>
  3. #include <vector>
  4. #include <typeinfo>
  5. #include <exception>
  6. using namespace std;
  7. int op;
  8.  
  9. class Abonament
  10. {
  11. protected:
  12. string nume_abonament;
  13. float pret;
  14. int perioada;
  15. public:
  16. Abonament(string nume="nume necunoscut", float pr=-1, int pe=-1)
  17. {
  18. nume_abonament=nume;
  19. pret=pr;
  20. pe=perioada;
  21. }
  22. Abonament(const Abonament &ob1)
  23. {
  24. nume_abonament=ob1.nume_abonament;
  25. pret=ob1.pret;
  26. perioada=ob1.perioada;
  27. }
  28. ~Abonament()
  29. {
  30. cout<<"un obiect de tip abonament a fost distrus"<<'\n';
  31. }
  32. string get_nume_abonament()
  33. {
  34. return nume_abonament;
  35. }
  36. float get_pret()
  37. {
  38. return pret;
  39. }
  40. int get_perioada()
  41. {
  42. return perioada;
  43. }
  44.  
  45. void set_nume_abonament(string nume)
  46. {
  47. nume_abonament=nume;
  48. }
  49. void set_pret(float pre)
  50. {
  51. pret=pre;
  52. }
  53. void set_perioada(int per)
  54. {
  55. perioada=per;
  56. }
  57.  
  58. friend ostream& operator <<(ostream &out, const Abonament& ob)
  59. {
  60. out<<"-Nume Abonament: "<<ob.nume_abonament<<'\n'<<"-Pret: "<<ob.pret<<'\n'<<"-Perioada: "<<ob.perioada<<'\n';
  61. return out;
  62. }
  63. friend istream& operator>>(istream& in, Abonament &ob)
  64. {
  65. cout<<"-Nume Abonament: ";
  66. in>>ob.nume_abonament;
  67. cout<<'\n';
  68. cout<<"-Pret: ";
  69. in>>ob.pret;
  70. cout<<'\n';
  71. cout<<"-Perioada: ";
  72. in>>ob.perioada;
  73. cout<<'\n';
  74. return in;
  75. }
  76. Abonament &operator =(Abonament &ob2)
  77. {
  78. if(this!=&ob2)
  79. {
  80. nume_abonament=ob2.nume_abonament;
  81. pret=ob2.pret;
  82. perioada=ob2.perioada;
  83. }
  84. return *this;
  85. }
  86. };
  87.  
  88. class Abonament_Premium:public Abonament
  89. {
  90. protected:
  91. int reducere;
  92. public:
  93. Abonament_Premium(string nume="nume necunoscut", float pr=-1, int pe=-1, int re=0): Abonament(nume,pr,re)
  94. {
  95. reducere=re;
  96. }
  97. Abonament_Premium(const Abonament_Premium &ob1): Abonament(ob1)
  98. {
  99. reducere=ob1.reducere;
  100. }
  101. ~Abonament_Premium()
  102. {
  103. cout<<"Un obiect de tip abonament premium a fost distrus"<<'\n';
  104. }
  105.  
  106. friend ostream& operator<<(ostream &out, const Abonament_Premium &ob)
  107. {
  108. out<<"-Nume Abonament: "<<ob.nume_abonament<<'\n'<<"-Pret: "<<ob.pret<<'\n'<<"-Perioada: "<<ob.perioada<<'\n'<<"-Reducere: "<<ob.reducere<<'\n';
  109. return out;
  110. }
  111. friend istream& operator>>(istream& in, Abonament_Premium &ob)
  112. {
  113. cout<<"-Nume Abonament: ";
  114. in>>ob.nume_abonament;
  115. cout<<'\n';
  116. cout<<"-Pret: ";
  117. in>>ob.pret;
  118. cout<<'\n';
  119. cout<<"-Perioada: ";
  120. in>>ob.perioada;
  121. cout<<'\n';
  122. cout<<"-Reducere: ";
  123. in>>ob.reducere;
  124. cout<<'\n';
  125. return in;
  126. }
  127. Abonament_Premium &operator =(Abonament_Premium &ob2)
  128. {
  129. if(this!=&ob2)
  130. {
  131. nume_abonament=ob2.nume_abonament;
  132. pret=ob2.pret;
  133. perioada=ob2.perioada;
  134. reducere=ob2.reducere;
  135. }
  136. return *this;
  137. }
  138. void set_reducere(int red)
  139. {
  140. reducere=red;
  141. }
  142. int get_reducere()
  143. {
  144. return reducere;
  145. }
  146. };
  147.  
  148. class Persoana
  149. {
  150. protected:
  151. int id;
  152. string nume;
  153. string cnp;
  154. public:
  155. Persoana(int i=-1, string n="Nume necunoscut", string c="cnp necunoscut")
  156. {
  157. id=i;
  158. nume=n;
  159. cnp=c;
  160. }
  161. Persoana(const Persoana &ob1)
  162. {
  163. id=ob1.id;
  164. nume=ob1.nume;
  165. cnp=ob1.cnp;
  166. }
  167. ~Persoana()
  168. {
  169. cout<<"Un obiect de tip persoana a fost distrus"<<'\n';
  170. }
  171. int get_id()
  172. {
  173. return id;
  174. }
  175. string get_nume()
  176. {
  177. return nume;
  178. }
  179. string get_cnp()
  180. {
  181. return cnp;
  182. }
  183.  
  184. void set_id(int i)
  185. {
  186. id=i;
  187. }
  188. void set_nume(string n)
  189. {
  190. nume=n;
  191. }
  192. void set_cnp(string c)
  193. {
  194. cnp=c;
  195. }
  196.  
  197. friend ostream& operator<<(ostream &out, const Persoana &ob1)
  198. {
  199. out<<"-Id persoana: "<<ob1.id<<'\n'<<"-Nume: "<<ob1.nume<<'\n'<<"-CNP: "<<ob1.cnp<<'\n';
  200. return out;
  201. }
  202. friend istream& operator>>(istream &in, Persoana &ob1)
  203. {
  204. cout<<"-Id: ";
  205. in>>ob1.id;
  206. cout<<'\n'<<"-Nume: ";
  207. in>>ob1.nume;
  208. cout<<'\n'<<"-CNP: ";
  209. in>>ob1.cnp;
  210. cout<<'\n';
  211. return in;
  212. }
  213. Persoana &operator=(Persoana &ob2)
  214. {
  215. if(this!=&ob2)
  216. {
  217. id=ob2.id;
  218. nume=ob2.nume;
  219. cnp=ob2.cnp;
  220. }
  221. return *this;
  222. }
  223. };
  224.  
  225. class Abonat:public Persoana
  226. {
  227. protected:
  228. string numar_telefon;
  229. Abonament x;
  230. public:
  231. Abonat(Abonament ab, int i=-1, string n="Nume necunoscut", string c="cnp necunoscut", string tel="Numar necunoscut"): Persoana(i,n,c)
  232. {
  233. numar_telefon=tel;
  234. x=ab;
  235. }
  236. Abonat(Abonat &ob1):Persoana(ob1)
  237. {
  238. numar_telefon=ob1.numar_telefon;
  239. x=ob1.x;
  240. x.set_nume_abonament(ob1.x.get_nume_abonament());
  241. x.set_pret(ob1.x.get_pret());
  242. x.set_perioada(ob1.x.get_perioada());
  243. }
  244. ~Abonat()
  245. {
  246. cout<<"Un obiect de tip Abonat a fost distrus"<<'\n';
  247. }
  248. friend ostream& operator<<(ostream &out, const Abonat &ob1)
  249. {
  250. out<<"-Id persoana: "<<ob1.id<<'\n'<<"-Nume: "<<ob1.nume<<'\n'<<"-CNP: "<<ob1.cnp<<'\n'<<"-Numar telefon: "<<ob1.numar_telefon<<'\n'<<"-Abonament: "<<ob1.x<<'\n';
  251. return out;
  252. }
  253. friend istream& operator>>(istream &in, Abonat &ob1)
  254. {
  255. cout<<"-Id: ";
  256. in>>ob1.id;
  257. cout<<'\n'<<"-Nume: ";
  258. in>>ob1.nume;
  259. cout<<'\n'<<"-CNP: ";
  260. in>>ob1.cnp;
  261. cout<<'\n';
  262. cout<<"-Numar de telefon: ";
  263. in>>ob1.numar_telefon;
  264. cout<<'\n';
  265. cout<<"-Abonament: ";
  266. in>>ob1.x;
  267. cout<<'\n';
  268. return in;
  269. }
  270. Abonat &operator=(Abonat &ob2)
  271. {
  272. if(this!=&ob2)
  273. {
  274. id=ob2.id;
  275. nume=ob2.nume;
  276. cnp=ob2.cnp;
  277. numar_telefon=ob2.numar_telefon;
  278. x=ob2.x;
  279. }
  280. return *this;
  281. }
  282.  
  283. void set_numar_telefon(string num)
  284. {
  285. numar_telefon=num;
  286. }
  287. void set_Abonament(Abonament &ob1)
  288. {
  289. x=ob1;
  290. }
  291. string get_numar_telefon()
  292. {
  293. return numar_telefon;
  294. }
  295. Abonament get_x()
  296. {
  297. return x;
  298. }
  299. };
  300. class Clienti
  301. {
  302. protected:
  303. //Abonament **abon = new Abonament *[200]();
  304. vector<Abonament*> abon[200];
  305. int nr_ab = 0;
  306. static int suma;
  307. public:
  308. static void suma_init(int val)
  309. {
  310. suma=val;
  311. }
  312. void nr_abb_inc(int n)
  313. {
  314. nr_ab=nr_ab+n;
  315. }
  316. void add_ab()
  317. {
  318. nr_ab++;
  319. Abonament *a;
  320. Abonament aux;
  321. cin>>aux;
  322. a = new Abonament(aux);
  323. //cout << nr_ab;
  324. cout<<"ajuns ";
  325. //cout<<abon[nr_ab];
  326. try
  327. {
  328. //abon[nr_ab]=a;
  329. abon[nr_ab].push_back(a);
  330. }
  331. catch(...)
  332. {
  333. cout<<"Probleme la alocarea memoriei. Adaugarea nu s-a putut efectua."<<'\n';
  334. }
  335. }
  336. void add_ab_prem()
  337. {
  338. nr_ab++;
  339. Abonament_Premium *b;
  340. Abonament_Premium aux;
  341. cin>>aux;
  342. b= new Abonament_Premium(aux); ///Pointerii mei sunt inacesibili. abon[nr_abb] face figuri
  343. try
  344. {
  345. abon[nr_ab].push_back(b);
  346. }
  347. catch(...)
  348. {
  349. cout<<"Probleme la alocarea memoriei. Adaugarea nu s-a putut efectua."<<'\n';
  350. }
  351. }
  352.  
  353. int nr_abon_premium()
  354. {
  355. int k=0;
  356. Abonament_Premium aux;
  357. for(int i=1; i<=nr_ab; i++)
  358. if(typeid(abon[i])==typeid(aux))
  359. k++;
  360. return k;
  361. }
  362. int get_suma()
  363. {
  364. for(int i=1; i<=nr_ab; i++)
  365. ///suma=suma+abon[i]->pret*abon[i]->perioada;
  366. return suma;
  367. }
  368. };
  369. int Clienti::suma;
  370. int main()
  371. {
  372. Clienti c;
  373. c.suma_init(0);
  374. cout<<"Acesta este meniul de navigare. Selectati optiunea pe care o doriti:"<<'\n';
  375. cout<<"1. Adauga abonati"<<'\n';
  376. cout<<"2. Afisare numar abonati premium"<<'\n';
  377. cout<<"3. Afisare suma de bani incasata de la toti abonatii curenti"<<'\n';
  378. cout<<"4. Inchide programul"<<'\n';
  379. while(op!=4)
  380. {
  381. cout<<"Optiunea dumneavoastra: ";
  382. cin>>op;
  383. cout<<'\n';
  384. if(op==1)
  385. {
  386. cout<<"Cati abonati doriti sa adaugati: ";
  387. int n;
  388. cin>>n;
  389. cout<<"Tipuri de abonamente:"<<'\n'<<"Standard = 1"<<'\n'<<"Premium = 2"<<'\n';
  390. for(int i=1; i<=n; i++)
  391. {
  392. cout<<"Abonatul "<<i<<" are abonament de tip: ";
  393. int tip;
  394. cin>>tip;
  395. try
  396. {
  397. switch(tip)
  398. {
  399. case 1:
  400. {
  401. c.add_ab();
  402. break;
  403. }
  404. case 2:
  405. {
  406. c.add_ab_prem();
  407. break;
  408. }
  409. }
  410. }
  411. catch(exception &e)
  412. {
  413. cout<<"S-a incercat si nu a mers. Eroare: "<<e.what()<<'\n';
  414. }
  415. ///suma
  416. }
  417. }
  418. if(op==2)
  419. {
  420. cout<<"Sunt "<<c.nr_abon_premium()<<" abonati premium"<<'\n';
  421. }
  422. if(op==3)
  423. {
  424. cout<<"Suma de bani incasata de la toti abonatii curenti este: "<<c.get_suma()<<'\n';
  425. }
  426. if(op==4)
  427. {
  428. cout<<"Programul s-a incheiat cu succes"<<'\n';
  429. }
  430. }
  431. return 0;
  432. }
  433.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement