19

 avatar
gorazd
c_cpp
a month ago
6.8 kB
2
Indexable
1kolok_OOP
#include <iostream>
#include <cstring>
#include  <iomanip>
using namespace std;
class IceCream {
    char *name;
    char sostav[100];
    float price;
    int sale;
public:
    IceCream(const char *_name = "", const char *_sostav = "", float _price = 0) {
        sale = 0;
        strcpy(sostav,_sostav);
        price = _price;
        if (_name != nullptr) {
            name = new char[strlen(_name)+1];
            strcpy(name,_name);
        }
        else
            name = nullptr;
    }
    ~IceCream(){delete[] name;}
    IceCream(const IceCream &i) {
        price = i.price;
        sale = i.sale;
        strcpy(sostav, i.sostav);
        if (i.name!=nullptr) {
            delete[] name;
            name = new char[strlen(i.name)+1];
            strcpy(name,i.name);
        }
        else
            name = nullptr;
    }
    float getNewPrice() const {
        return (price * (1 - sale/100.0));
    }
    void setDiscount(int discount){sale = discount;}
    void setName(const char *name) {
        if (this->name!=nullptr)
            delete[] this->name;
        this->name = new char[strlen(name)+1];
        strcpy(this->name,name);
    }
    void setPrice(float price){this->price = price;}
    IceCream &operator++() {
        sale += 5;
        return *this;
    }
    IceCream operator+(const char *_new) {
        int len1 = strlen(name);
        int len2 = strlen(_new);
        char *newName = new char[len1 + len2 + 2];
        strcpy(newName,name);
        strcat(newName," + ");
        strcat(newName, _new);
        IceCream result(newName);
        strcpy(result.sostav,sostav);
        result.price = price+10;
        result.sale = sale;
        return result;
    }
    IceCream &operator=(const IceCream &i){
        price = i.price;
        sale = i.sale;
        strcpy(sostav, i.sostav);
        if (i.name!=nullptr) {
            delete[] name;
            name = new char[strlen(i.name)+1];
            strcpy(name,i.name);
        }
        else
            name = nullptr;
        return *this;
    }
    friend ostream &operator<<(ostream &os, const IceCream &ice);
};
ostream &operator<<(ostream &os, const IceCream &ice) {
    os << ice.name<<": "<<ice.sostav<<" "<<ice.price;
    if (ice.sale>0)
        os << " ("<<ice.getNewPrice()<<")";
    return os;
}
class IceCreamShop {
    char name[50];
    IceCream *ICE;
    int n;
public:
    IceCreamShop(const char *_name) {
        strcpy(name,_name);
        ICE = nullptr;
        n = 0;
    }
    ~IceCreamShop() {
        delete[] ICE;
    }
    IceCreamShop(const IceCreamShop &ic) {
        strcpy(name,ic.name);
        n = ic.n;
        if (ic.ICE!=nullptr and ic.n>0) {
            delete[] ICE;
            IceCream *tmp = new IceCream[n];
            for (int i = 0; i<n; i++) {
                tmp[i] = ic.ICE[i];
            }
            ICE = tmp;
        }
        else {
            ICE = nullptr;
            n = 0;
        }
    }
    IceCreamShop &operator=(const IceCreamShop &ic) {
        strcpy(name,ic.name);
        n = ic.n;
        if (ic.ICE!=nullptr and ic.n>0) {
            delete[] ICE;
            IceCream *tmp = new IceCream[n];
            for (int i = 0; i<n; i++) {
                tmp[i] = ic.ICE[i];
            }
            ICE = tmp;
        }
        else {
            ICE = nullptr;
            n = 0;
        }
        return *this;
    }
    IceCreamShop &operator+=(const IceCream &i) {
        IceCream *tmp = new IceCream[n+1];
        for (int j = 0; j<n; j++)
            tmp[j] = ICE[j];
        tmp[n] = i;
        delete[] ICE;
        ICE = tmp;
        n++;
        return *this;
    }
    friend ostream &operator<<(ostream &os,const IceCreamShop &ice);
};

ostream &operator<<(ostream &os,const IceCreamShop &ice) {
    os<<ice.name<<endl;
    for (int i = 0; i<ice.n; i++) {
        os << ice.ICE[i] << endl;
    }
    return os;
};

int main() {
char name[100];
    char ingr[100];
    float price;
    int discount;

    int testCase;

    cin >> testCase;
	cin.get();
    if(testCase == 1) {
        cout << "====== TESTING IceCream CLASS ======" << endl;
        cin.getline(name,100);
        cin.getline(ingr,100);
        cin >> price;
        cin >> discount;
        cout << "CONSTRUCTOR" << endl;
        IceCream ic1(name, ingr, price);
        ic1.setDiscount(discount);
        cin.get();
        cin.getline(name,100);
        cin.getline(ingr,100);
        cin >> price;
        cin >> discount;
        IceCream ic2(name, ingr, price);
        ic2.setDiscount(discount);
        cout << "OPERATOR <<" << endl;
        cout << ic1 << endl;
        cout << ic2 << endl;
        cout << "OPERATOR ++" << endl;
        ++ic1;
        cout << ic1 << endl;
        cout << "OPERATOR +" << endl;
        IceCream ic3 = ic2 + "chocolate";
        cout << ic3 << endl;
    } else if(testCase == 2) {
        cout << "====== TESTING IceCream CONSTRUCTORS ======" << endl;
        cin.getline(name,100);
        cin.getline(ingr,100);
        cin >> price;
        cout << "CONSTRUCTOR" << endl;
        IceCream ic1(name, ingr, price);
        cout << ic1 << endl;
        cout << "COPY CONSTRUCTOR" << endl;
        IceCream ic2(ic1);
        cin.get();
        cin.getline(name,100);
        ic2.setName(name);
        cout << ic1 << endl;
        cout << ic2 << endl;
        cout << "OPERATOR =" << endl;
        ic1 = ic2;
        cin.getline(name,100);
        ic2.setName(name);
        cout << ic1 << endl;
        cout << ic2 << endl;

        cin >> discount;
        ic1.setDiscount(discount);


    } else if(testCase == 3) {
        cout << "====== TESTING IceCreamShop ======" << endl;
        char icsName[50];
        cin.getline(icsName,100);
        cout << "CONSTRUCTOR" << endl;
        IceCreamShop ics(icsName);
        int n;
        cin >> n;
        cout << "OPERATOR +=" << endl;
        for(int i = 0; i < n; ++i) {
            cin.get();
            cin.getline(name,100);
        	cin.getline(ingr,100);
            cin >> price;
            IceCream ic(name, ingr, price);
            ics += ic;
        }
        cout << ics;
    } else if(testCase == 4) {
        cout << "====== TESTING IceCreamShop CONSTRUCTORS ======" << endl;
        char icsName[50];
        cin.getline(icsName,100);
        IceCreamShop ics(icsName);
        int n;
        cin >> n;
        for(int i = 0; i < n; ++i) {
            cin.get();
            cin.getline(name,100);
        	cin.getline(ingr,100);
            cin >> price;
            IceCream ic(name, ingr, price);
            ics += ic;
        }
        IceCream x("FINKI fruits", "strawberry ice cream, raspberry ice cream, blueberry ice cream", 60);
        IceCreamShop icp = ics;
        ics+=x;
        cout << ics << endl;
        cout << icp << endl;
    }


    return 0;
}
Editor is loading...
Leave a Comment