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 euro;
- static float dollar;
- virtual float price()=0;
- virtual float discount_price()=0;
- virtual void print();
- };
- float Discount::euro=61.5;
- float Discount::dollar=54.5;
- class Product{
- protected:
- float productPrice;
- char *name;
- public:
- Product(){}
- Product(char *name,float productPrice){
- this->name=new char[strlen(name)+1];
- strcpy(this->name,name);
- this->productPrice=productPrice;
- }
- // set method
- void changePrice(float newPrice){
- if(newPrice<0){
- throw NegativeValueException("Product can't have a negative value");
- }
- productPrice=newPrice;
- }
- virtual ~Product(){}
- };
- class FoodProduct:public Product,public Discount{
- private:
- float weight;
- public:
- FoodProduct(){}
- FoodProduct(char *name,float productPrice):Product(name,productPrice){}
- FoodProduct(char *name,float productPrice,float weight):Product(name,productPrice){
- this->weight=weight;
- }
- //
- float discount_price(){
- return productPrice;
- }
- void print(){
- cout<<"FoodProduct"<<endl;
- }
- float price(){
- return discount_price();
- }
- };
- class Drinks:public Product,public Discount{
- private:
- char *brand;
- bool alcoholic;
- public:
- Drinks(){}
- Drinks(char *name,float productPrice,char *brand,bool alcoholic):Product(name,productPrice){
- strcpy(this->brand,brand);
- this->alcoholic=alcoholic;
- }
- //
- float discount_price(){
- if(alcoholic && (productPrice/Discount::euro)>20){
- return productPrice*0.95;
- }else if(alcoholic==false && strcmp(brand,"Coca-Cola")==0){
- return productPrice*0.9;
- }else{
- return productPrice;
- }
- }
- void print(){
- cout<<"Drinks"<<endl;
- }
- float price(){
- return discount_price();
- }
- };
- class Cosmetics:public Product,public Discount{
- private:
- float weight;
- public:
- Cosmetics(){}
- Cosmetics(char *name,float productPrice,float weight):Product(name,productPrice){
- this->weight=weight;
- }
- //
- float discount_price(){
- if((productPrice/Discount::dollar)>20){
- return productPrice*0.86;
- }else if((productPrice/Discount::euro)>5){
- return productPrice*0.88;
- }else{
- return productPrice;
- }
- }
- void print_rule(){
- cout<<"Cosmetics"<<endl;
- }
- float price(){
- return discount_price();
- }
- };
- float total_discount(Discount **products,int n){
- float totalDiscount=0;
- for(int i=0;i<n;i++){
- totalDiscount+=products[i]->discount_price();
- }
- return totalDiscount;
- }
- int main() {
- int n = 0;
- float newPrice;
- 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);
- cout << "Vkupnata cena na site proizvodi e: " << total_discount(d, n) << endl;
- //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 object){
- object.print();
- }
- }
- }
- for (int i=0; i < n; ++i){
- delete d[i];
- }
- delete[] d;
- return 0;
- }
- // static - moze da se povikuva bez objekt
- /*
- class Exception{
- private:
- char *error;
- public:
- Exception(){}
- Exception(char *error){
- this->error=new char[strlen(error)+1];
- strcpy(this->error,error);
- }
- void print(){
- cout<<error<<endl;
- }
- };
- class Zivotno{
- protected:
- char boja[15];
- int godini;
- static int broj;
- public:
- Zivotno(){
- cout<<"Default constructor zivotno"<<endl;
- }
- Zivotno(char boja[],int godini){
- cout<<"Parameter constructor zivotno"<<endl;
- broj++;
- strcpy(this->boja,boja);
- this->godini=godini;
- }
- // virtuelna funkcija
- virtual void print(){
- cout<<"Zivotno: "<<boja<<" "<<godini<<endl;
- }
- // cisto virtuelna funkcija
- virtual float damage()=0;
- // cisto virtuelna funkcija
- virtual char tip()=0;
- static int getBroj(){
- throw 301;
- return broj;
- }
- static void setBroj(int x){
- broj=x;
- }
- virtual ~Zivotno(){
- cout<<"Default destructor zivotno"<<endl;
- }
- };
- int Zivotno::broj=0;
- class Kuce:public Zivotno{
- private:
- char rasa[15];
- public:
- Kuce(){
- //cout<<"Default constructor Kuce"<<endl;
- }
- Kuce(char boja[],int godini,char rasa[]):Zivotno(boja,godini){
- //cout<<"Parameter constructor Kuce"<<endl;
- strcpy(this->boja,boja);
- this->godini=godini;
- strcpy(this->rasa,rasa);
- }
- // override na zivotno funkcijata
- void print(){
- cout<<"Kuce: "<<boja<<" "<<godini<<" "<<rasa<<endl;
- }
- // 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
- float damage(){
- if(godini>1 && godini<=5){
- return 30;
- }else if(godini>5){
- return 20;
- }else{
- return 10;
- }
- }
- //
- char tip(){
- return 'k';
- }
- };
- class Mace:public Zivotno{
- private:
- bool daliPrede;
- public:
- Mace(){
- //cout<<"Default constructor Mace"<<endl;
- }
- Mace(char boja[],int godini,bool daliPrede):Zivotno(boja,godini){
- this->daliPrede=daliPrede;
- }
- // preoptovaruvame virtuelna funkcija
- void print(){
- Zivotno::print();
- cout<<daliPrede<<endl;
- }
- // preoptovaruvame cisto virtuelna funkcija
- float damage(){
- if(daliPrede){
- return 1;
- }else{
- return 11;
- }
- }
- char tip(){
- return 'm';
- }
- ~Mace(){
- cout<<"Default destructor Mace"<<endl;
- }
- };
- void printZivotni(Zivotno **niza,int n){
- int flag=1;
- for(int i=0;i<n;i++){
- if(niza[i]->damage()<10){
- cout<<"Ne pravat site zivotni nad 10 damage"<<endl;
- flag=0;
- break;
- }
- }
- if(flag==1){
- cout<<"Site zivotni pravat nad 10 damage"<<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<<"Najvekje damage pravi zivotnoto:"<<endl;
- niza[pamtiIndex]->print();
- }
- void odnosZivotni(Zivotno **niza,int n){
- int brojKucinja=0;
- int brojMacinja=0;
- Kuce *pK;
- Mace *pM;
- for(int i=0;i<n;i++){
- pK=dynamic_cast<Kuce*>(niza[i]);
- if(pK!=0){
- brojKucinja++;
- }
- pM=dynamic_cast<Mace*>(niza[i]);
- if(pM!=0){
- brojMacinja++;
- }
- // bez dynamic cast so posebna cisto virtuelna funkcija
- if(niza[i]->tip()=='k'){
- brojKucinja++;
- }else if(niza[i]->tip()=='m'){
- brojMacinja++;
- }
- }
- cout<<"Kucinja "<<brojKucinja<<":"<<brojMacinja<<" Macinja"<<endl;
- }
- int main(){
- // obicna niza od zivotni
- //Zivotno niza[3];
- // dinamicki alocirana niza od zivotni
- //Zivotno *niza=new Zivotno[3];
- // dinamicki alocirana niza od pokazuvaci
- Zivotno **niza=new Zivotno*[3];
- niza[0]=new Kuce("crno",15,"labrador");
- niza[1]=new Kuce("crno-kafeavo",4,"d.o.dz");
- niza[2]=new Mace("belo",2,true);
- // povik za static promenliva bez objekt
- //Zivotno::broj=2;
- try{
- Zivotno::setBroj(10001);
- Zivotno::getBroj();
- }catch(Exception objekt){
- objekt.print();
- }catch(int x){
- cout<<x<<endl;
- }
- // static get
- cout<<Zivotno::getBroj()<<endl;
- //static set
- cout<<niza[1]->getBroj()<<endl;
- //printZivotni(niza,3);
- //najvekjeDamage(niza,3);
- //odnosZivotni(niza,3);
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment