Advertisement
Guest User

Untitled

a guest
May 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. class Consumable
  5. {
  6. protected:
  7.     bool opened;
  8. public:
  9.     virtual void readLabel()=0;
  10.     virtual void consume()=0;
  11.     virtual double quantity()=0;
  12. };
  13. class Eatable : public Consumable
  14. {
  15. protected:
  16.     double massPerPiece;
  17.     int pieces;
  18. public:
  19.     Eatable(double _massPerPiece=-1,int _pieces=-1)
  20.     {
  21.         this->opened=0;
  22.         this->massPerPiece=_massPerPiece;
  23.         this->pieces=_pieces;
  24.     }
  25.  
  26.     void readLabel()
  27.     {
  28.         cout<<"Opened: "<<this->opened<<endl;
  29.         cout<<"Mass: "<<this->massPerPiece<<endl;
  30.         cout<<"Pieces: "<<this->pieces<<endl;
  31.     }
  32.     void consume()
  33.     {
  34.         this->opened=1;
  35.         this->pieces--;
  36.     }
  37.     double quantity()
  38.     {
  39.         return this->pieces;
  40.     }
  41. };
  42. class Drinkable : public Consumable
  43. {
  44. protected:
  45.     double volume;
  46. public:
  47.     Drinkable(double _volume=-1)
  48.     {
  49.         opened=0;
  50.         this->volume=_volume;
  51.     }
  52.  
  53.     void readLabel()
  54.     {
  55.         cout<<"Opened: "<<opened<<endl;
  56.         cout<<"Volume: "<<this->volume<<endl;
  57.     }
  58.     void consume()
  59.     {
  60.         opened=1;
  61.         this->volume-=5;
  62.         if(this->volume<0) this->volume = 0;
  63.     }
  64.     double quantity()
  65.     {
  66.        return this->volume;
  67.     }
  68. };
  69.  
  70. class Chocolate : public Eatable
  71. {
  72.     double cocoa;
  73.     int rows,cols;
  74. public:
  75.     Chocolate(double _cocoa,int _rows,int _cols,double _massPerPiece,int _pieces)
  76.     {
  77.         this->cocoa=_cocoa;
  78.         this->rows=_rows;
  79.         this->cols=_cols;
  80.         this->massPerPiece=_massPerPiece;
  81.         this->pieces=_pieces;
  82.     }
  83.     void readLabel()
  84.     {
  85.         cout<<"Cocoa: "<<this->cocoa<<endl;
  86.         cout<<"Rows: "<<this->rows<<endl;
  87.         cout<<"Cols: "<<this->cols<<endl;
  88.         cout<<"Mass per piece: "<<this->massPerPiece<<endl;
  89.         cout<<"Pieces: "<<this->pieces<<endl;
  90.     }
  91.     void consume()
  92.     {
  93.         this->opened=1;
  94.         this->pieces--;
  95.         if(this->pieces<0) this->pieces=0;
  96.     }
  97.     double quantity()
  98.     {
  99.         return this->pieces;
  100.     }
  101. };
  102. class Beer : public Drinkable
  103. {
  104.     int alcohol;
  105.     string brand;
  106. public:
  107.     Beer(int _alcohol,string _brand,double _volume)
  108.     {
  109.         this->alcohol=_alcohol;
  110.         this->brand=_brand;
  111.         this->volume=_volume;
  112.     }
  113.     void readLabel()
  114.     {
  115.         cout<<"Alcohol: "<<this->alcohol<<endl;
  116.         cout<<"Brand: "<<this->brand<<endl;
  117.         cout<<"Volume: "<<this->volume<<endl;
  118.         cout<<"Opened: "<<this->opened<<endl;
  119.     }
  120.     void consume()
  121.     {
  122.         this->opened=1;
  123.         this->volume-=5;
  124.         if(this->volume<0) this->volume=0;
  125.     }
  126.     double quantity()
  127.     {
  128.         return this->volume;
  129.     }
  130. };
  131.  
  132. template<class T>
  133. class VendingMacine
  134. {
  135.     vector<vector<T*>> v;
  136. public:
  137.     static int sn;
  138.     VendingMacine(size_t sz)
  139.     {
  140.         v = vector<vector<T*>>(sz);
  141.         sn++;
  142.     }
  143.  
  144.     T operator [] (size_t idx)
  145.     {
  146.         if(idx<v.size())
  147.         {
  148.             T ans = v[idx].back();
  149.             v[idx].pop_back();
  150.             return ans;
  151.         }
  152.     }
  153.     void put(size_t idx,T* Tptr)
  154.     {
  155.         if(idx<v.size())
  156.         {
  157.             v[idx].push_back(Tptr);
  158.         }
  159.     }
  160.     bool empty(size_t idx)
  161.     {
  162.         return v[idx].empty();
  163.     }
  164.  
  165. };
  166.  
  167. template<class T>
  168. int VendingMacine<T>::sn=0;
  169. int main()
  170. {
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement