borisdexter

Isklucoci i staticki prom

Jan 24th, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class NegativeValueException{
  5. private:
  6. char error[50];
  7. public:
  8. NegativeValueException(){}
  9. NegativeValueException(char error[]){
  10. strcpy(this->error,error);
  11. }
  12. void print(){
  13. cout<<error<<endl;
  14. }
  15. };
  16. class Discount{
  17. public:
  18. static float euro;
  19. static float dollar;
  20. virtual float price()=0;
  21. virtual float discount_price()=0;
  22. virtual void print();
  23. };
  24. float Discount::euro=61.5;
  25. float Discount::dollar=54.5;
  26.  
  27. class Product{
  28. protected:
  29. float productPrice;
  30. char *name;
  31. public:
  32. Product(){}
  33. Product(char *name,float productPrice){
  34. this->name=new char[strlen(name)+1];
  35. strcpy(this->name,name);
  36. this->productPrice=productPrice;
  37. }
  38. // set method
  39. void changePrice(float newPrice){
  40. if(newPrice<0){
  41. throw NegativeValueException("Product can't have a negative value");
  42. }
  43. productPrice=newPrice;
  44. }
  45.  
  46. virtual ~Product(){}
  47. };
  48.  
  49. class FoodProduct:public Product,public Discount{
  50. private:
  51. float weight;
  52. public:
  53. FoodProduct(){}
  54. FoodProduct(char *name,float productPrice):Product(name,productPrice){}
  55. FoodProduct(char *name,float productPrice,float weight):Product(name,productPrice){
  56. this->weight=weight;
  57. }
  58. //
  59. float discount_price(){
  60. return productPrice;
  61. }
  62.  
  63. void print(){
  64. cout<<"FoodProduct"<<endl;
  65. }
  66.  
  67. float price(){
  68. return discount_price();
  69. }
  70.  
  71. };
  72.  
  73. class Drinks:public Product,public Discount{
  74. private:
  75. char *brand;
  76. bool alcoholic;
  77. public:
  78. Drinks(){}
  79. Drinks(char *name,float productPrice,char *brand,bool alcoholic):Product(name,productPrice){
  80. strcpy(this->brand,brand);
  81. this->alcoholic=alcoholic;
  82. }
  83. //
  84. float discount_price(){
  85. if(alcoholic && (productPrice/Discount::euro)>20){
  86. return productPrice*0.95;
  87. }else if(alcoholic==false && strcmp(brand,"Coca-Cola")==0){
  88. return productPrice*0.9;
  89. }else{
  90. return productPrice;
  91. }
  92. }
  93.  
  94. void print(){
  95. cout<<"Drinks"<<endl;
  96. }
  97.  
  98. float price(){
  99. return discount_price();
  100. }
  101. };
  102.  
  103. class Cosmetics:public Product,public Discount{
  104. private:
  105. float weight;
  106. public:
  107. Cosmetics(){}
  108. Cosmetics(char *name,float productPrice,float weight):Product(name,productPrice){
  109. this->weight=weight;
  110. }
  111. //
  112. float discount_price(){
  113. if((productPrice/Discount::dollar)>20){
  114. return productPrice*0.86;
  115. }else if((productPrice/Discount::euro)>5){
  116. return productPrice*0.88;
  117. }else{
  118. return productPrice;
  119. }
  120. }
  121.  
  122. void print_rule(){
  123. cout<<"Cosmetics"<<endl;
  124. }
  125.  
  126. float price(){
  127. return discount_price();
  128. }
  129.  
  130. };
  131.  
  132. float total_discount(Discount **products,int n){
  133. float totalDiscount=0;
  134. for(int i=0;i<n;i++){
  135. totalDiscount+=products[i]->discount_price();
  136. }
  137. return totalDiscount;
  138. }
  139.  
  140. int main() {
  141. int n = 0;
  142. float newPrice;
  143. Discount **d = new Discount*[10];
  144. d[n++] = new FoodProduct("leb", 30);
  145. d[n++] = new Drinks("viski", 1350, "Jack Daniel's", true);
  146. d[n++] = new FoodProduct("sirenje", 390, 105);
  147. d[n++] = new Drinks("votka", 850, "Finlandia", true);
  148. d[n++] = new Cosmetics("krema", 720, 100);
  149. d[n++] = new Drinks("sok", 50, "Coca-Cola", false);
  150. d[n++] = new Cosmetics("parfem", 3500, 50);
  151. cout << "Vkupnata cena na site proizvodi e: " << total_discount(d, n) << endl;
  152. //se menuva cenata na site Kozmeticki proizvodi
  153. cout << "Promena na cenata na kozmetickite proizvodi " << endl;
  154. for (int i = 0; i < n; ++i) {
  155. Cosmetics* c = dynamic_cast<Cosmetics *>(d[i]);
  156. if (c != 0){
  157. c->print();
  158. cin >> newPrice;
  159. try{
  160. c->changePrice(newPrice);
  161. }catch(NegativeValueException object){
  162. object.print();
  163. }
  164. }
  165. }
  166.  
  167. for (int i=0; i < n; ++i){
  168. delete d[i];
  169. }
  170. delete[] d;
  171. return 0;
  172.  
  173. }
  174.  
  175.  
  176. // static - moze da se povikuva bez objekt
  177. /*
  178. class Exception{
  179. private:
  180. char *error;
  181. public:
  182. Exception(){}
  183. Exception(char *error){
  184. this->error=new char[strlen(error)+1];
  185. strcpy(this->error,error);
  186. }
  187. void print(){
  188. cout<<error<<endl;
  189. }
  190. };
  191.  
  192. class Zivotno{
  193. protected:
  194. char boja[15];
  195. int godini;
  196. static int broj;
  197. public:
  198.  
  199. Zivotno(){
  200. cout<<"Default constructor zivotno"<<endl;
  201. }
  202. Zivotno(char boja[],int godini){
  203. cout<<"Parameter constructor zivotno"<<endl;
  204. broj++;
  205. strcpy(this->boja,boja);
  206. this->godini=godini;
  207. }
  208. // virtuelna funkcija
  209. virtual void print(){
  210. cout<<"Zivotno: "<<boja<<" "<<godini<<endl;
  211. }
  212. // cisto virtuelna funkcija
  213. virtual float damage()=0;
  214. // cisto virtuelna funkcija
  215. virtual char tip()=0;
  216.  
  217. static int getBroj(){
  218. throw 301;
  219. return broj;
  220. }
  221.  
  222. static void setBroj(int x){
  223.  
  224. broj=x;
  225. }
  226.  
  227. virtual ~Zivotno(){
  228. cout<<"Default destructor zivotno"<<endl;
  229. }
  230.  
  231. };
  232. int Zivotno::broj=0;
  233.  
  234. class Kuce:public Zivotno{
  235. private:
  236. char rasa[15];
  237. public:
  238. Kuce(){
  239. //cout<<"Default constructor Kuce"<<endl;
  240. }
  241. Kuce(char boja[],int godini,char rasa[]):Zivotno(boja,godini){
  242. //cout<<"Parameter constructor Kuce"<<endl;
  243. strcpy(this->boja,boja);
  244. this->godini=godini;
  245. strcpy(this->rasa,rasa);
  246. }
  247. // override na zivotno funkcijata
  248. void print(){
  249. cout<<"Kuce: "<<boja<<" "<<godini<<" "<<rasa<<endl;
  250. }
  251. // ako e nad 5 godini, prai 20 damage, ako e nad 1 i pod 5 godini prai 30 damage, ako e pod 1 godina prai 10 damage
  252. float damage(){
  253. if(godini>1 && godini<=5){
  254. return 30;
  255. }else if(godini>5){
  256. return 20;
  257. }else{
  258. return 10;
  259. }
  260. }
  261.  
  262. //
  263. char tip(){
  264. return 'k';
  265. }
  266.  
  267. };
  268.  
  269. class Mace:public Zivotno{
  270. private:
  271. bool daliPrede;
  272. public:
  273. Mace(){
  274. //cout<<"Default constructor Mace"<<endl;
  275. }
  276. Mace(char boja[],int godini,bool daliPrede):Zivotno(boja,godini){
  277. this->daliPrede=daliPrede;
  278. }
  279. // preoptovaruvame virtuelna funkcija
  280. void print(){
  281. Zivotno::print();
  282. cout<<daliPrede<<endl;
  283. }
  284. // preoptovaruvame cisto virtuelna funkcija
  285. float damage(){
  286. if(daliPrede){
  287. return 1;
  288. }else{
  289. return 11;
  290. }
  291. }
  292.  
  293. char tip(){
  294. return 'm';
  295. }
  296. ~Mace(){
  297. cout<<"Default destructor Mace"<<endl;
  298. }
  299. };
  300.  
  301. void printZivotni(Zivotno **niza,int n){
  302. int flag=1;
  303. for(int i=0;i<n;i++){
  304. if(niza[i]->damage()<10){
  305. cout<<"Ne pravat site zivotni nad 10 damage"<<endl;
  306. flag=0;
  307. break;
  308. }
  309. }
  310. if(flag==1){
  311. cout<<"Site zivotni pravat nad 10 damage"<<endl;
  312. }
  313. }
  314.  
  315. void najvekjeDamage(Zivotno **niza,int n){
  316. float maxDamage=0;
  317. int pamtiIndex;
  318. for(int i=0;i<n;i++){
  319. if(niza[i]->damage()>maxDamage){
  320. maxDamage=niza[i]->damage();
  321. pamtiIndex=i;
  322. }
  323. }
  324. cout<<"Najvekje damage pravi zivotnoto:"<<endl;
  325. niza[pamtiIndex]->print();
  326. }
  327.  
  328. void odnosZivotni(Zivotno **niza,int n){
  329. int brojKucinja=0;
  330. int brojMacinja=0;
  331. Kuce *pK;
  332. Mace *pM;
  333. for(int i=0;i<n;i++){
  334.  
  335. pK=dynamic_cast<Kuce*>(niza[i]);
  336. if(pK!=0){
  337. brojKucinja++;
  338. }
  339. pM=dynamic_cast<Mace*>(niza[i]);
  340. if(pM!=0){
  341. brojMacinja++;
  342. }
  343. // bez dynamic cast so posebna cisto virtuelna funkcija
  344.  
  345. if(niza[i]->tip()=='k'){
  346. brojKucinja++;
  347. }else if(niza[i]->tip()=='m'){
  348. brojMacinja++;
  349. }
  350.  
  351. }
  352. cout<<"Kucinja "<<brojKucinja<<":"<<brojMacinja<<" Macinja"<<endl;
  353. }
  354.  
  355. int main(){
  356. // obicna niza od zivotni
  357. //Zivotno niza[3];
  358. // dinamicki alocirana niza od zivotni
  359. //Zivotno *niza=new Zivotno[3];
  360. // dinamicki alocirana niza od pokazuvaci
  361. Zivotno **niza=new Zivotno*[3];
  362. niza[0]=new Kuce("crno",15,"labrador");
  363. niza[1]=new Kuce("crno-kafeavo",4,"d.o.dz");
  364. niza[2]=new Mace("belo",2,true);
  365.  
  366. // povik za static promenliva bez objekt
  367. //Zivotno::broj=2;
  368.  
  369. try{
  370. Zivotno::setBroj(10001);
  371. Zivotno::getBroj();
  372. }catch(Exception objekt){
  373. objekt.print();
  374. }catch(int x){
  375. cout<<x<<endl;
  376. }
  377.  
  378.  
  379.  
  380. // static get
  381. cout<<Zivotno::getBroj()<<endl;
  382. //static set
  383. cout<<niza[1]->getBroj()<<endl;
  384. //printZivotni(niza,3);
  385. //najvekjeDamage(niza,3);
  386. //odnosZivotni(niza,3);
  387. return 0;
  388. }
  389. */
Advertisement
Add Comment
Please, Sign In to add comment