Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. template <typename T>
  5. class Device
  6. {
  7. protected :
  8.     char* marka;
  9.     T* prilojeniq;
  10.     int count;
  11.     void copy(const char*,int);
  12. public :
  13.     Device(const char* = " ",int = 0);
  14.     Device(const Device<T>&);
  15.     Device<T>& operator=(const Device<T>&);
  16.     ~Device();
  17.      void print() const;
  18. };
  19. template <typename T>
  20. void Device<T>::print() const
  21. {
  22.     cout << marka << " " << count << endl;
  23. }
  24. template <typename T>
  25. void Device<T>::copy(const char* marka,int count)
  26. {
  27.     this->marka = new char[strlen(marka) + 1];
  28.     strcpy(this->marka,marka);
  29.     this->count = count;
  30. }
  31. template <typename T>
  32. Device<T>::Device(const char* marka,int count)
  33. {
  34.     copy(marka,count);
  35.     prilojeniq = new T[count];
  36.     for(int i = 0;i < count;i++)
  37.     {
  38.         prilojeniq[i] = 0;
  39.     }
  40. }
  41. template <typename T>
  42. Device<T>::Device(const Device<T>& other)
  43. {
  44.     copy(other.marka,other.count);
  45.     for(int i = 0;i < count;i++)
  46.     {
  47.         prilojeniq[i] = other.prilojeniq[i];
  48.     }
  49. }
  50. template <typename T>
  51. Device<T>& Device<T>::operator=(const Device<T>& other)
  52. {
  53.     if(this != &other)
  54.     {
  55.         delete[] prilojeniq;
  56.         delete[] marka;
  57.         copy(other.marka,other.count);
  58.         for(int i = 0;i < count;i++)
  59.         {
  60.             prilojeniq[i] = other.prilojeniq[i];
  61.         }
  62.     }
  63.     return *this;
  64. }
  65. template <typename T>
  66. Device<T>::~Device()
  67. {
  68.     delete[] marka;
  69.     delete[] prilojeniq;
  70. }
  71. template <typename T>
  72. class Phone : public Device<T>
  73. {
  74. private :
  75.     double price;
  76. public :
  77.     Phone(double = 0,const char* = " ",int = 0);
  78.     void infoButon() const;
  79.     bool CompareWith(const Phone<T>&);
  80.     bool operator>(const Phone<T>&);
  81.     void print() const;
  82. };
  83. template <typename T>
  84. void Phone<T>::print() const
  85. {
  86.     cout << price << " " << marka << " " << count << endl;
  87. }
  88. template <typename T>
  89. bool Phone<T>::operator>(const Phone<T>& phone)
  90. {
  91.     return this->price > phone.price && this->count > phone.count;
  92. }
  93. template <typename T>
  94. bool Phone<T>::CompareWith(const Phone<T>& phone)
  95. {
  96.     return *this > phone;
  97. }
  98. template <typename T>
  99. Phone<T>::Phone(double price,const char* marka,int count) : Device<T>(marka,count)
  100. {
  101.     this->price = price;
  102. }
  103. template <typename T>
  104. void Phone<T>::infoButon() const
  105. {
  106.    Device<T>::print();
  107. }
  108. template <typename T>
  109. class Catalog
  110. {
  111. private :
  112.     Phone<T>* phones;
  113.     int count;
  114. public :
  115.     Catalog(int = 0);
  116.     Catalog(const Catalog<T>&);
  117.     Catalog<T>& operator=(const Catalog<T>&);
  118.     ~Catalog();
  119.     void addPhones(const Phone<T>&);
  120.     void print() const;
  121. };
  122. template <typename T>
  123. void Catalog<T>::print() const
  124. {
  125.     for(int i = 0;i < count;i++)
  126.     {
  127.         phones[i].print();
  128.     }
  129. }
  130. template <typename T>
  131. void Catalog<T>::addPhones(const Phone<T>& phone)
  132. {
  133.     phones[count++] = phone;
  134. }
  135. template <typename T>
  136. Catalog<T>::Catalog(int count)
  137. {
  138.     this->count = count;
  139.     phones = new Phone<T>[count];
  140. }
  141. template <typename T>
  142. Catalog<T>::Catalog(const Catalog<T>& other)
  143. {
  144.     this->count = count;
  145.     for(int i = 0;i < count;i++)
  146.     {
  147.         this->phones[i] = other.phones[i];
  148.     }
  149. }
  150. template <typename T>
  151. Catalog<T>& Catalog<T>::operator=(const Catalog<T>& other)
  152. {
  153.     if(this != &other)
  154.     {
  155.         delete[] phones;
  156.         this->count = count;
  157.         for(int i = 0;i < count;i++)
  158.         {
  159.             phones[i] = other.phones[i];
  160.         }
  161.     }
  162.     return *this;
  163. }
  164. template <typename T>
  165. Catalog<T>::~Catalog()
  166. {
  167.     delete[] phones;
  168. }
  169. int main()
  170. {
  171.     Catalog<double> catalog(5);
  172.     Phone<double> a(1200.60,"LG",15);
  173.     Phone<double> b(999.99,"Samsung",6);
  174.     Phone<double> c(1800.99,"Lenovo",20);
  175.     Phone<double> d(2000.99,"Huawei",18);
  176.     catalog.addPhones(a);
  177.     catalog.addPhones(b);
  178.     catalog.addPhones(c);
  179.     catalog.addPhones(d);
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement