Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class IceCream {
- private:
- char *ime;
- char sostav[100];
- float cena;
- int popust;
- public:
- IceCream(const char *ime="", const char *sostav="", float cena=0.0){
- this->ime = new char[strlen(ime)+1];
- strcpy(this->ime, ime);
- strcpy(this->sostav, sostav);
- this->cena = cena;
- this->popust = 0;
- }
- IceCream &operator=(const IceCream &rhs){
- this->ime = new char[strlen(rhs.ime)+1];
- strcpy(this->ime, rhs.ime);
- strcpy(this->sostav, rhs.sostav);
- this->cena = rhs.cena;
- this->popust = rhs.popust;
- return *this;
- }
- IceCream ic(const IceCream &rhs){
- this->ime = new char[strlen(rhs.ime)+1];
- strcpy(this->ime, rhs.ime);
- strcpy(this->sostav, rhs.sostav);
- this->cena = rhs.cena;
- this->popust = rhs.popust;
- }
- int setDiscount(int popust){
- this->popust = popust;
- return this->popust;
- }
- void setName(const char* n) {
- ime = new char[strlen(n) + 1];
- strcpy(ime, n);
- }
- ~IceCream(){ }
- friend ostream &operator << (ostream &o, IceCream &rhs){
- if(rhs.popust>0)
- o << rhs.ime << ": " << rhs.sostav << " " << rhs.cena << " (" << (float)rhs.cena-(rhs.cena*rhs.popust*0.01) << ")" << " ";
- else o << rhs.ime << ": " << rhs.sostav << " " << rhs.cena << " ";
- return o;
- }
- IceCream &operator++(){
- this->popust+=5;
- return *this;
- }
- IceCream &operator+(const char string[]){
- strcat(this->ime, " + ");
- strcat(this->ime, string);
- this->cena+=10;
- return *this;
- }
- };
- class IceCreamShop {
- private:
- char ime[50];
- IceCream *ic;
- int brSladoledi;
- public:
- IceCreamShop(const char *ime=""){
- strcpy(this->ime, ime);
- brSladoledi = 0;
- }
- ~IceCreamShop(){}
- IceCreamShop &operator+=(const IceCream &rhs){
- IceCream *temp = new IceCream[brSladoledi+1];
- for(int i=0; i<brSladoledi; i++)
- temp[i] = ic[i];
- temp[brSladoledi]=rhs;
- ic = temp;
- brSladoledi++; return *this;
- }
- friend ostream &operator << (ostream &o, IceCreamShop &rhs){
- o << rhs.ime << endl;
- for(int i=0; i<rhs.brSladoledi; i++)
- cout << rhs.ic[i] << endl;
- return o;
- }
- };
- 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;
- //cin >> discount;
- 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;
- }
Add Comment
Please, Sign In to add comment