Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Product
  6. {
  7.     friend ostream& operator<< (ostream& levo, const Product& desno);
  8. private:
  9.     int imeLen;
  10.     char * ime;
  11.     int cena;
  12. public:
  13.     Product();
  14.     Product(const char* _ime, int _cena);
  15.     ~Product();
  16.     Product(Product& p);
  17.     Product& operator=(Product &p);
  18.     const char* GetIme() const { return ime; };
  19.     int GetCena() const { return cena; };
  20.     bool operator== (const Product& desno);
  21. };
  22.  
  23. Product::Product()
  24. {
  25.     ime = NULL;
  26.     cena = 0;
  27. }
  28.  
  29. Product::Product(const char* _ime, int _cena)
  30. {
  31.     imeLen = strlen(_ime);
  32.     ime = new char[imeLen+1];
  33.     strcpy(ime, _ime);
  34.     cena = _cena;
  35. }
  36.  
  37. Product::~Product()
  38. {
  39.     delete[] ime;
  40. }
  41.  
  42. Product::Product(Product &p)
  43. {
  44.     imeLen = p.imeLen;
  45.     ime = new char[imeLen+1];
  46.     strcpy(ime, p.ime);
  47.     cena = p.cena;
  48. }
  49.  
  50. Product& Product::operator =(Product &p)
  51. {
  52.     if (this == &p) return *this;
  53.     imeLen = p.imeLen;
  54.     delete[] this->ime;
  55.     ime = new char[imeLen+1];
  56.     strcpy(ime, p.ime);
  57.     cena = p.cena;
  58.     return *this;
  59.  
  60. }
  61.  
  62. bool Product::operator ==(const Product &desno)
  63. {
  64.     if (this->cena != desno.cena)
  65.         return false;
  66.     if (strcmp(this->ime, desno.ime) != 0)
  67.         return false;
  68.  
  69.     return true;
  70. }
  71.  
  72. ostream& operator<< (ostream& levo, const Product& desno)
  73. {
  74.     levo << desno.ime << ' ' << desno.cena << endl;
  75.     return levo;
  76. }
  77.  
  78. class Basket
  79. {
  80. private:
  81.     Product * niza;
  82.     int len;
  83.     int elementi;
  84.     Basket(Basket& b);
  85.     Basket& operator=(Basket& desno);
  86.     //bool ZgolemiNiza(int newLen);
  87. public:
  88.     Basket(int _len=10);
  89.     ~Basket();
  90.     int GetElementi() { return elementi; }
  91.     float Average();
  92.     int Total();
  93.     Basket& operator+=(Product& desno);
  94. };
  95.  
  96. Basket::Basket(int _len)
  97. {
  98.     len = _len;
  99.     niza = new Product[10];
  100.     elementi = 0;
  101. }
  102.  
  103. Basket::~Basket()
  104. {
  105.     delete[] niza;
  106. }
  107.  
  108. Basket& Basket::operator +=(Product& desno)
  109. {
  110.     if (elementi<len)
  111.     {
  112.         niza[elementi] = desno;
  113.         elementi++;
  114.     }
  115.     return *this;
  116. }
  117.  
  118. int Basket::Total()
  119. {
  120.     int ret = 0;
  121.     for (int i=0; i<elementi; i++)
  122.     {
  123.         ret+=niza[i].GetCena();
  124.     }
  125.     return ret;
  126. }
  127.  
  128. float Basket::Average()
  129. {
  130.     return Total()*1.0F/elementi;
  131. }
  132.  
  133. int main()
  134. {
  135.     Product p1("Coca Cula", 60);
  136.     Product p2("Pupsi", 50);
  137.     Product p3("Pivo 1.5l", 90);
  138.     Basket b;
  139.     b += p1;
  140.     b += p2;
  141.     b += p3;
  142.     cout << "prosecna cena:" << b.Average() << endl;
  143.     return 0;
  144. }
Add Comment
Please, Sign In to add comment