Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- void string_copy(char* dest, const char* source, int length) {
- strncpy(dest, source, length);
- dest[length] = 0;
- }
- class IceCream {
- char* ime;
- char sostav[100];
- float cena;
- int popust;
- public:
- IceCream() {
- ime = 0;
- strcpy(sostav, "\0");
- cena = 0.0;
- popust = 0;
- }
- IceCream(const char* _ime, const char* _sostav, const float _cena) {
- ime = new char[strlen(_ime+1)];
- string_copy(ime, _ime, strlen(_ime));
- string_copy(sostav, _sostav, strlen(_sostav));
- cena = _cena;
- popust = 0;
- }
- IceCream(const IceCream& obj) {
- ime = new char[strlen(obj.ime+1)];
- string_copy(ime, obj.ime, strlen(obj.ime));
- string_copy(sostav, obj.sostav, 99);
- cena = obj.cena;
- popust = obj.popust;
- }
- IceCream& operator=(const IceCream& obj) {
- if(this != &obj) {
- delete [] ime;
- ime = new char[strlen(obj.ime+1)];
- string_copy(ime, obj.ime, strlen(obj.ime));
- string_copy(sostav, obj.sostav, 99);
- cena = obj.cena;
- popust = obj.popust;
- }
- return *this;
- }
- IceCream& operator++() {
- popust += 5;
- return *this;
- }
- IceCream operator+(const char* niza) {
- IceCream obj(*this);
- delete [] obj.ime;
- obj.ime = new char[strlen(ime) + strlen(" + ") + strlen(niza)];
- strcat(obj.ime, ime);
- strcat(obj.ime, " + ");
- strcat(obj.ime, niza);
- obj.ime[strlen(obj.ime)] = 0;
- obj.cena += 10;
- return obj;
- };
- void setDiscount(int discount) { popust = discount; }
- void setName(char* name) {
- delete [] ime;
- ime = new char[strlen(name)+1];
- string_copy(ime, name, strlen(name));
- }
- friend ostream& operator<<(ostream& out, const IceCream& obj) {
- out << obj.ime << ": " << obj.sostav << " " << obj.cena;
- if(obj.popust > 0) {
- out << " (" << (obj.cena - ((obj.popust/100.0)*obj.cena)) << ")";
- return out;
- }
- out << " ";
- return out;
- }
- ~IceCream() {
- delete [] ime;
- }
- };
- class IceCreamShop {
- char ime[50];
- IceCream* sladoledi;
- int vkupno_sladoledi;
- public:
- IceCreamShop(const char* _ime = "") {
- string_copy(ime, _ime, 49);
- sladoledi = NULL;
- vkupno_sladoledi = 0;
- }
- IceCreamShop(const IceCreamShop& obj) {
- string_copy(ime, obj.ime, 49);
- vkupno_sladoledi = obj.vkupno_sladoledi;
- sladoledi = new IceCream[vkupno_sladoledi];
- for(int i = 0; i < vkupno_sladoledi; ++i) {
- sladoledi[i] = obj.sladoledi[i];
- }
- }
- IceCreamShop& operator=(const IceCreamShop& obj) {
- if(this != &obj) {
- delete [] sladoledi;
- string_copy(ime, obj.ime, 49);
- vkupno_sladoledi = obj.vkupno_sladoledi;
- sladoledi = new IceCream[vkupno_sladoledi];
- for(int i = 0; i < vkupno_sladoledi; ++i) {
- sladoledi[i] = obj.sladoledi[i];
- }
- }
- return *this;
- }
- IceCreamShop& operator+=(const IceCream& obj) {
- IceCream* temp = new IceCream[vkupno_sladoledi+1];
- for(int i = 0; i < vkupno_sladoledi; ++i) {
- temp[i] = sladoledi[i];
- }
- delete [] sladoledi;
- sladoledi = temp;
- sladoledi[vkupno_sladoledi++] = obj;
- return *this;
- }
- friend ostream& operator<<(ostream& out, const IceCreamShop& obj) {
- out << obj.ime << endl;
- for(int i = 0; i < obj.vkupno_sladoledi; ++i) {
- cout << obj.sladoledi[i] << endl;
- }
- return out;
- }
- ~IceCreamShop() {
- delete [] sladoledi;
- }
- };
- 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;
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement