borisdexter

Objektno isklucoci i staticki promenlivi

Jan 24th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class NegativeValueException{
  4. private:
  5. char error[50];
  6. public:
  7. NegativeValueException(){}
  8. NegativeValueException(char error[]){
  9. strcpy(this->error,error);
  10. }
  11. void print(){
  12. cout<<error<<endl;
  13. }
  14. };
  15.  
  16. class Discount{
  17. public:
  18. static float evro;
  19. static float dolar;
  20. // site proizvodi imaat popust od 10 procenti
  21. virtual float discountAmount(){
  22. return 10;
  23. }
  24. // mora sekoj shto nasleduva da ja ima ovaa funkcija
  25. virtual float price()=0;
  26. virtual void print()=0;
  27.  
  28. };
  29. float Discount::evro=61.5;
  30. float Discount::dolar=55.1;
  31.  
  32. class Product{
  33. protected:
  34. float cena;
  35. char *ime;
  36. public:
  37. Product(){
  38. this->ime=new char[0];
  39. }
  40. Product(char *ime,float cena){
  41. this->ime=new char[strlen(ime)+1];
  42. strcpy(this->ime,ime);
  43. this->cena=cena;
  44. }
  45.  
  46. void changePrice(float novaCena){
  47. if(novaCena<0){
  48. throw NegativeValueException("Ne moze negativna vrednost");
  49. }
  50. cena=novaCena;
  51. }
  52.  
  53. ~Product(){
  54. delete []ime;
  55. }
  56. };
  57.  
  58. class FoodProduct:public Product,public Discount{
  59. private:
  60. float gramaza;
  61. public:
  62. FoodProduct(){}
  63. FoodProduct(char *ime,float cena):Product(ime,cena){}
  64. FoodProduct(char *ime,float cena,float gramaza):Product(ime,cena){
  65. this->gramaza=gramaza;
  66. }
  67.  
  68. //float discountAmount(){}
  69. // mora sekoj shto nasleduva da ja ima ovaa funkcija
  70. float price(){
  71. return cena - cena/Discount::discountAmount();
  72. }
  73. void print(){
  74. cout<<ime<<" "<<cena<<" "<<gramaza<<endl;
  75. }
  76. };
  77.  
  78. class Drinks:public Product,public Discount{
  79. private:
  80. char brend[25];
  81. bool daliAlkoholen;
  82. public:
  83. Drinks(){}
  84. Drinks(char *ime,float cena,char *brend,bool daliAlkoholen):Product(ime,cena){
  85. strcpy(this->brend,brend);
  86. this->daliAlkoholen=daliAlkoholen;
  87. }
  88.  
  89. float discountAmount(){
  90. if(daliAlkoholen && (cena/Discount::evro)>20){
  91. return Discount::discountAmount()+5;
  92. }else if(!daliAlkoholen && strcmp(brend,"Coca-Cola")==0){
  93. return Discount::discountAmount()+10;
  94. }else{
  95. return Discount::discountAmount();
  96. }
  97. }
  98. // mora sekoj shto nasleduva da ja ima ovaa funkcija
  99. float price(){
  100. return cena - cena/Discount::discountAmount();
  101. }
  102. void print(){
  103. cout<<ime<<" "<<cena<<" "<<brend<<" "<<daliAlkoholen<<endl;
  104. }
  105. };
  106.  
  107. class Cosmetics:public Product,public Discount{
  108. private:
  109. float miliLitri;
  110. public:
  111. Cosmetics(){}
  112. Cosmetics(char *ime,float cena,float miliLitri):Product(ime,cena){
  113. this->miliLitri=miliLitri;
  114. }
  115.  
  116.  
  117.  
  118. float discountAmount(){
  119. if((cena/Discount::dolar)>20){
  120. return Discount::discountAmount() + 14;
  121. }else if((cena/Discount::evro)>5){
  122. return Discount::discountAmount() + 12;
  123. }else{
  124. return Discount::discountAmount();
  125. }
  126. }
  127.  
  128. // mora sekoj shto nasleduva da ja ima ovaa funkcija
  129. float price(){
  130. return cena - cena/Discount::discountAmount();
  131. }
  132. void print(){
  133. cout<<ime<<" "<<cena<<" "<<miliLitri<<endl;
  134. }
  135. };
  136.  
  137. void printAllProducts(Discount **niza,int n){
  138. for(int i=0;i<n;i++){
  139. niza[i]->print();
  140. }
  141. }
  142.  
  143. void total_discount(Discount **niza,int n){
  144. float vkupnaCena=0;
  145. for(int i=0;i<n;i++){
  146. niza[i]->print();
  147. cout<<"Treba da dobie popust od "<<niza[i]->discountAmount()<<"% "<<niza[i]->price()<<endl;
  148. vkupnaCena+=niza[i]->price();
  149. }
  150. cout<<"Vkupnata cena na site proizvodi e "<<vkupnaCena<<endl;
  151. }
  152.  
  153. int main(){
  154. float newPrice;
  155. int n=0;
  156. Discount **d = new Discount*[10];
  157. d[n++] = new FoodProduct("leb", 30);
  158. d[n++] = new Drinks("viski", 1350, "Jack Daniel's", true);
  159. d[n++] = new FoodProduct("sirenje", 390, 105);
  160. d[n++] = new Drinks("votka", 850, "Finlandia", true);
  161. d[n++] = new Cosmetics("krema", 720, 100);
  162. d[n++] = new Drinks("sok", 50, "Coca-Cola", false);
  163. d[n++] = new Cosmetics("parfem", 3500, 50);
  164.  
  165. //printAllProducts(d,n);
  166. total_discount(d,n);
  167. //se menuva cenata na site Kozmeticki proizvodi
  168.  
  169. cout<<"Promena na cenata na kozmetickite proizvodi "<<endl;
  170. for (int i=0;i<n;++i) {
  171. Cosmetics* c = dynamic_cast<Cosmetics *>(d[i]);
  172. if (c != 0){
  173. c->print();
  174. cin>>newPrice;
  175. try{
  176. c->changePrice(newPrice);
  177. }catch(NegativeValueException objekt){
  178. objekt.print();
  179. }
  180. }
  181. }
  182.  
  183. delete []d;
  184. return 0;
  185. }
  186.  
  187. /*
  188. // static - moze da se povikuva i bez objekt
  189. class Exception{
  190. private:
  191. char error[50];
  192. public:
  193. Exception(){}
  194. Exception(char error[]){
  195. strcpy(this->error,error);
  196. }
  197.  
  198. void print(){
  199. cout<<error<<endl;
  200. }
  201. };
  202. class Zivotno{
  203. protected:
  204. char boja[15];
  205. float tezina;
  206. static int broj;
  207. public:
  208.  
  209. Zivotno(){
  210. //cout<<"Default constructor Zivotno"<<endl;
  211. }
  212. Zivotno(char boja[],float tezina){
  213. broj++;
  214. cout<<"Parameter constructor Zivotno"<<endl;
  215. strcpy(this->boja,boja);
  216. this->tezina=tezina;
  217. }
  218. // obicna virtuelna funkcija
  219. virtual void print(){
  220. cout<<"Zivotno print()"<<endl;
  221. }
  222. // cisto virtuelna funkcija
  223. virtual float damage()=0;
  224. //
  225. virtual char tip()=0;
  226.  
  227. static int getBroj(){
  228. //throw Exception("Error getBroj()");
  229. return broj;
  230. }
  231.  
  232. static void setBroj(int novBroj){
  233. if(novBroj<0){
  234. throw Exception("Error setBroj()");
  235. }
  236. broj=novBroj;
  237. }
  238.  
  239. char* getBoja(){
  240. return boja;
  241. }
  242.  
  243. ~Zivotno(){
  244. cout<<"Default destructor Zivotno"<<endl;
  245. }
  246. };
  247.  
  248. int Zivotno::broj=0;
  249.  
  250. class Kuce:public Zivotno{
  251. private:
  252. char rasa[15];
  253. bool daliKasa;
  254. public:
  255. Kuce(){
  256. //cout<<"Default constructor Kuce"<<endl;
  257. }
  258. Kuce(char boja[],float tezina,char rasa[],bool daliKasa):Zivotno(boja,tezina){
  259. //cout<<"Parameter constructor Kuce"<<endl;
  260. // ovie gi pishuvame ako nemavme constructor od Zivotno
  261. //strcpy(this->boja,boja);
  262. //this->tezina=tezina;
  263. strcpy(this->rasa,rasa);
  264. this->daliKasa=daliKasa;
  265. }
  266.  
  267. // preoptovaruvame cisto virtuelnata funkcija
  268. float damage(){
  269. if(tezina>20){
  270. return 30;
  271. }else{
  272. return 15;
  273. }
  274. }
  275.  
  276. void print(){
  277. Zivotno::print();
  278. cout<<rasa<<" "<<daliKasa<<endl;
  279. }
  280.  
  281. char tip(){
  282. return 'k';
  283. }
  284. ~Kuce(){
  285. //cout<<"Default destructor Kuce"<<endl;
  286. }
  287. };
  288.  
  289. class Petel:public Zivotno{
  290. private:
  291. bool daliKukurika;
  292. public:
  293. Petel(){
  294. //cout<<"Default constructor Petel"<<endl;
  295. }
  296. Petel(char boja[],float tezina,bool daliKukurika):Zivotno(boja,tezina){
  297. //cout<<"Parameter constructor Petel"<<endl;
  298. // ovie gi pishuvame ako nemavme constructor od Zivotno
  299. //strcpy(this->boja,boja);
  300. //this->tezina=tezina;
  301. this->daliKukurika=daliKukurika;
  302. }
  303.  
  304. // override na Zivotno.print();
  305. void print(){
  306. cout<<"Petel "<<boja<<" "<<tezina<<" "<<daliKukurika<<endl;
  307. }
  308.  
  309. char tip(){
  310. return 'p';
  311. }
  312.  
  313. float damage(){
  314. if(daliKukurika){
  315. return 10;
  316. }else{
  317. return 5;
  318. }
  319. }
  320.  
  321. ~Petel(){
  322. //cout<<"Default destructor Petel"<<endl;
  323. }
  324. };
  325.  
  326. void printZivotni(Zivotno **niza,int n){
  327. for(int i=0;i<n;i++){
  328. niza[i]->print();
  329. cout<<niza[i]->damage()<<endl;
  330. }
  331. }
  332.  
  333. void kuceVSpetel(Zivotno **niza,int n){
  334. int brojKucinja=0;
  335. int brojPetli=0;
  336. for(int i=0;i<n;i++){
  337. // prva opcija
  338. Kuce *pK=dynamic_cast<Kuce*>(niza[i]);
  339. // ako pokazuvacot ne e 0 uspeshno sme kastirale
  340. if(pK!=0){
  341. brojKucinja++;
  342. }
  343. Petel *pP=dynamic_cast<Petel*>(niza[i]);
  344. if(pP!=0){
  345. brojPetli++;
  346. }
  347. // vtora opcija
  348. if(niza[i]->tip()=='k'){
  349. brojKucinja++;
  350. }else if(niza[i]->tip()=='p'){
  351. brojPetli++;
  352. }
  353. }
  354. cout<<"Kuce "<<brojKucinja<<":"<<brojPetli<<" Petel"<<endl;
  355. }
  356.  
  357.  
  358. void najvekjeDamage(Zivotno **niza,int n){
  359. float maxDamage=0;
  360. int pamtiIndex;
  361. for(int i=0;i<n;i++){
  362. if(niza[i]->damage()>maxDamage){
  363. maxDamage=niza[i]->damage();
  364. pamtiIndex=i;
  365. }
  366. }
  367. cout<<"Zivotno so najvekje damage"<<endl;
  368. niza[pamtiIndex]->print();
  369. }
  370.  
  371. void vkupenDamage(Zivotno **niza,int n){
  372. float suma=0;
  373. for(int i=0;i<n;i++){
  374. suma+=niza[i]->damage();
  375. }
  376. cout<<"Prosecniot damage e "<<suma/n<<endl;
  377. }
  378.  
  379. void siteZivotniNadX(Zivotno **niza,int n,int x){
  380. int flag=1;
  381. for(int i=0;i<n;i++){
  382. if(niza[i]->damage()<=x){
  383. flag=0;
  384. break;
  385. }
  386. }
  387. if(flag==1){
  388. cout<<"Site praat nad "<<x<<" damage"<<endl;
  389. }else{
  390. cout<<"Ne praat site nad "<<x<<" damage"<<endl;
  391. }
  392. }
  393.  
  394. void siteBeliZivotni(Zivotno **niza,int n){
  395. for(int i=0;i<n;i++){
  396. if(strcmp(niza[i]->getBoja(),"bela")==0){
  397. niza[i]->print();
  398. }
  399. }
  400. }
  401. int main(){
  402. // obicna zivotno niza
  403. //Zivotno niza[3];
  404. // dinamicki alocirana niza
  405. //Zivotno *niza=new Zivotno[3];
  406. Zivotno **niza=new Zivotno*[4];
  407. niza[0]=new Kuce("crna",15,"dzukela",false);
  408. niza[1]=new Petel("bela",2,true);
  409. niza[2]=new Kuce("bela",20,"labrador",true);
  410. niza[3]=new Petel("crvena",5,false);
  411.  
  412. try{
  413. Zivotno::setBroj(-5);
  414. }catch(Exception objekt){
  415. objekt.print();
  416. }
  417. cout<<Zivotno::getBroj()<<endl;
  418.  
  419. //printZivotni(niza,4);
  420. //kuceVSpetel(niza,4);
  421. //najvekjeDamage(niza,4);
  422. //vkupenDamage(niza,4);
  423. //siteZivotniNadX(niza,4,10);
  424. //siteBeliZivotni(niza,4);
  425. return 0;
  426. }
  427. */
Advertisement
Add Comment
Please, Sign In to add comment