Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class NegativeValueException{
- private:
- char error[50];
- public:
- NegativeValueException(){}
- NegativeValueException(char error[]){
- strcpy(this->error,error);
- }
- void print(){
- cout<<error<<endl;
- }
- };
- class Discount{
- public:
- static float evro;
- static float dolar;
- // site proizvodi imaat popust od 10 procenti
- virtual float discountAmount(){
- return 10;
- }
- // mora sekoj shto nasleduva da ja ima ovaa funkcija
- virtual float price()=0;
- virtual void print()=0;
- };
- float Discount::evro=61.5;
- float Discount::dolar=55.1;
- class Product{
- protected:
- float cena;
- char *ime;
- public:
- Product(){
- this->ime=new char[0];
- }
- Product(char *ime,float cena){
- this->ime=new char[strlen(ime)+1];
- strcpy(this->ime,ime);
- this->cena=cena;
- }
- void changePrice(float novaCena){
- if(novaCena<0){
- throw NegativeValueException("Ne moze negativna vrednost");
- }
- cena=novaCena;
- }
- ~Product(){
- delete []ime;
- }
- };
- class FoodProduct:public Product,public Discount{
- private:
- float gramaza;
- public:
- FoodProduct(){}
- FoodProduct(char *ime,float cena):Product(ime,cena){}
- FoodProduct(char *ime,float cena,float gramaza):Product(ime,cena){
- this->gramaza=gramaza;
- }
- //float discountAmount(){}
- // mora sekoj shto nasleduva da ja ima ovaa funkcija
- float price(){
- return cena - cena/Discount::discountAmount();
- }
- void print(){
- cout<<ime<<" "<<cena<<" "<<gramaza<<endl;
- }
- };
- class Drinks:public Product,public Discount{
- private:
- char brend[25];
- bool daliAlkoholen;
- public:
- Drinks(){}
- Drinks(char *ime,float cena,char *brend,bool daliAlkoholen):Product(ime,cena){
- strcpy(this->brend,brend);
- this->daliAlkoholen=daliAlkoholen;
- }
- float discountAmount(){
- if(daliAlkoholen && (cena/Discount::evro)>20){
- return Discount::discountAmount()+5;
- }else if(!daliAlkoholen && strcmp(brend,"Coca-Cola")==0){
- return Discount::discountAmount()+10;
- }else{
- return Discount::discountAmount();
- }
- }
- // mora sekoj shto nasleduva da ja ima ovaa funkcija
- float price(){
- return cena - cena/Discount::discountAmount();
- }
- void print(){
- cout<<ime<<" "<<cena<<" "<<brend<<" "<<daliAlkoholen<<endl;
- }
- };
- class Cosmetics:public Product,public Discount{
- private:
- float miliLitri;
- public:
- Cosmetics(){}
- Cosmetics(char *ime,float cena,float miliLitri):Product(ime,cena){
- this->miliLitri=miliLitri;
- }
- float discountAmount(){
- if((cena/Discount::dolar)>20){
- return Discount::discountAmount() + 14;
- }else if((cena/Discount::evro)>5){
- return Discount::discountAmount() + 12;
- }else{
- return Discount::discountAmount();
- }
- }
- // mora sekoj shto nasleduva da ja ima ovaa funkcija
- float price(){
- return cena - cena/Discount::discountAmount();
- }
- void print(){
- cout<<ime<<" "<<cena<<" "<<miliLitri<<endl;
- }
- };
- void printAllProducts(Discount **niza,int n){
- for(int i=0;i<n;i++){
- niza[i]->print();
- }
- }
- void total_discount(Discount **niza,int n){
- float vkupnaCena=0;
- for(int i=0;i<n;i++){
- niza[i]->print();
- cout<<"Treba da dobie popust od "<<niza[i]->discountAmount()<<"% "<<niza[i]->price()<<endl;
- vkupnaCena+=niza[i]->price();
- }
- cout<<"Vkupnata cena na site proizvodi e "<<vkupnaCena<<endl;
- }
- int main(){
- float newPrice;
- int n=0;
- Discount **d = new Discount*[10];
- d[n++] = new FoodProduct("leb", 30);
- d[n++] = new Drinks("viski", 1350, "Jack Daniel's", true);
- d[n++] = new FoodProduct("sirenje", 390, 105);
- d[n++] = new Drinks("votka", 850, "Finlandia", true);
- d[n++] = new Cosmetics("krema", 720, 100);
- d[n++] = new Drinks("sok", 50, "Coca-Cola", false);
- d[n++] = new Cosmetics("parfem", 3500, 50);
- //printAllProducts(d,n);
- total_discount(d,n);
- //se menuva cenata na site Kozmeticki proizvodi
- cout<<"Promena na cenata na kozmetickite proizvodi "<<endl;
- for (int i=0;i<n;++i) {
- Cosmetics* c = dynamic_cast<Cosmetics *>(d[i]);
- if (c != 0){
- c->print();
- cin>>newPrice;
- try{
- c->changePrice(newPrice);
- }catch(NegativeValueException objekt){
- objekt.print();
- }
- }
- }
- delete []d;
- return 0;
- }
- /*
- // static - moze da se povikuva i bez objekt
- class Exception{
- private:
- char error[50];
- public:
- Exception(){}
- Exception(char error[]){
- strcpy(this->error,error);
- }
- void print(){
- cout<<error<<endl;
- }
- };
- class Zivotno{
- protected:
- char boja[15];
- float tezina;
- static int broj;
- public:
- Zivotno(){
- //cout<<"Default constructor Zivotno"<<endl;
- }
- Zivotno(char boja[],float tezina){
- broj++;
- cout<<"Parameter constructor Zivotno"<<endl;
- strcpy(this->boja,boja);
- this->tezina=tezina;
- }
- // obicna virtuelna funkcija
- virtual void print(){
- cout<<"Zivotno print()"<<endl;
- }
- // cisto virtuelna funkcija
- virtual float damage()=0;
- //
- virtual char tip()=0;
- static int getBroj(){
- //throw Exception("Error getBroj()");
- return broj;
- }
- static void setBroj(int novBroj){
- if(novBroj<0){
- throw Exception("Error setBroj()");
- }
- broj=novBroj;
- }
- char* getBoja(){
- return boja;
- }
- ~Zivotno(){
- cout<<"Default destructor Zivotno"<<endl;
- }
- };
- int Zivotno::broj=0;
- class Kuce:public Zivotno{
- private:
- char rasa[15];
- bool daliKasa;
- public:
- Kuce(){
- //cout<<"Default constructor Kuce"<<endl;
- }
- Kuce(char boja[],float tezina,char rasa[],bool daliKasa):Zivotno(boja,tezina){
- //cout<<"Parameter constructor Kuce"<<endl;
- // ovie gi pishuvame ako nemavme constructor od Zivotno
- //strcpy(this->boja,boja);
- //this->tezina=tezina;
- strcpy(this->rasa,rasa);
- this->daliKasa=daliKasa;
- }
- // preoptovaruvame cisto virtuelnata funkcija
- float damage(){
- if(tezina>20){
- return 30;
- }else{
- return 15;
- }
- }
- void print(){
- Zivotno::print();
- cout<<rasa<<" "<<daliKasa<<endl;
- }
- char tip(){
- return 'k';
- }
- ~Kuce(){
- //cout<<"Default destructor Kuce"<<endl;
- }
- };
- class Petel:public Zivotno{
- private:
- bool daliKukurika;
- public:
- Petel(){
- //cout<<"Default constructor Petel"<<endl;
- }
- Petel(char boja[],float tezina,bool daliKukurika):Zivotno(boja,tezina){
- //cout<<"Parameter constructor Petel"<<endl;
- // ovie gi pishuvame ako nemavme constructor od Zivotno
- //strcpy(this->boja,boja);
- //this->tezina=tezina;
- this->daliKukurika=daliKukurika;
- }
- // override na Zivotno.print();
- void print(){
- cout<<"Petel "<<boja<<" "<<tezina<<" "<<daliKukurika<<endl;
- }
- char tip(){
- return 'p';
- }
- float damage(){
- if(daliKukurika){
- return 10;
- }else{
- return 5;
- }
- }
- ~Petel(){
- //cout<<"Default destructor Petel"<<endl;
- }
- };
- void printZivotni(Zivotno **niza,int n){
- for(int i=0;i<n;i++){
- niza[i]->print();
- cout<<niza[i]->damage()<<endl;
- }
- }
- void kuceVSpetel(Zivotno **niza,int n){
- int brojKucinja=0;
- int brojPetli=0;
- for(int i=0;i<n;i++){
- // prva opcija
- Kuce *pK=dynamic_cast<Kuce*>(niza[i]);
- // ako pokazuvacot ne e 0 uspeshno sme kastirale
- if(pK!=0){
- brojKucinja++;
- }
- Petel *pP=dynamic_cast<Petel*>(niza[i]);
- if(pP!=0){
- brojPetli++;
- }
- // vtora opcija
- if(niza[i]->tip()=='k'){
- brojKucinja++;
- }else if(niza[i]->tip()=='p'){
- brojPetli++;
- }
- }
- cout<<"Kuce "<<brojKucinja<<":"<<brojPetli<<" Petel"<<endl;
- }
- void najvekjeDamage(Zivotno **niza,int n){
- float maxDamage=0;
- int pamtiIndex;
- for(int i=0;i<n;i++){
- if(niza[i]->damage()>maxDamage){
- maxDamage=niza[i]->damage();
- pamtiIndex=i;
- }
- }
- cout<<"Zivotno so najvekje damage"<<endl;
- niza[pamtiIndex]->print();
- }
- void vkupenDamage(Zivotno **niza,int n){
- float suma=0;
- for(int i=0;i<n;i++){
- suma+=niza[i]->damage();
- }
- cout<<"Prosecniot damage e "<<suma/n<<endl;
- }
- void siteZivotniNadX(Zivotno **niza,int n,int x){
- int flag=1;
- for(int i=0;i<n;i++){
- if(niza[i]->damage()<=x){
- flag=0;
- break;
- }
- }
- if(flag==1){
- cout<<"Site praat nad "<<x<<" damage"<<endl;
- }else{
- cout<<"Ne praat site nad "<<x<<" damage"<<endl;
- }
- }
- void siteBeliZivotni(Zivotno **niza,int n){
- for(int i=0;i<n;i++){
- if(strcmp(niza[i]->getBoja(),"bela")==0){
- niza[i]->print();
- }
- }
- }
- int main(){
- // obicna zivotno niza
- //Zivotno niza[3];
- // dinamicki alocirana niza
- //Zivotno *niza=new Zivotno[3];
- Zivotno **niza=new Zivotno*[4];
- niza[0]=new Kuce("crna",15,"dzukela",false);
- niza[1]=new Petel("bela",2,true);
- niza[2]=new Kuce("bela",20,"labrador",true);
- niza[3]=new Petel("crvena",5,false);
- try{
- Zivotno::setBroj(-5);
- }catch(Exception objekt){
- objekt.print();
- }
- cout<<Zivotno::getBroj()<<endl;
- //printZivotni(niza,4);
- //kuceVSpetel(niza,4);
- //najvekjeDamage(niza,4);
- //vkupenDamage(niza,4);
- //siteZivotniNadX(niza,4,10);
- //siteBeliZivotni(niza,4);
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment