Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- template <typename T>
- class Device
- {
- protected :
- char* marka;
- T* prilojeniq;
- int count;
- void copy(const char*,int);
- public :
- Device(const char* = " ",int = 0);
- Device(const Device<T>&);
- Device<T>& operator=(const Device<T>&);
- ~Device();
- void print() const;
- };
- template <typename T>
- void Device<T>::print() const
- {
- cout << marka << " " << count << endl;
- }
- template <typename T>
- void Device<T>::copy(const char* marka,int count)
- {
- this->marka = new char[strlen(marka) + 1];
- strcpy(this->marka,marka);
- this->count = count;
- }
- template <typename T>
- Device<T>::Device(const char* marka,int count)
- {
- copy(marka,count);
- prilojeniq = new T[count];
- for(int i = 0;i < count;i++)
- {
- prilojeniq[i] = 0;
- }
- }
- template <typename T>
- Device<T>::Device(const Device<T>& other)
- {
- copy(other.marka,other.count);
- for(int i = 0;i < count;i++)
- {
- prilojeniq[i] = other.prilojeniq[i];
- }
- }
- template <typename T>
- Device<T>& Device<T>::operator=(const Device<T>& other)
- {
- if(this != &other)
- {
- delete[] prilojeniq;
- delete[] marka;
- copy(other.marka,other.count);
- for(int i = 0;i < count;i++)
- {
- prilojeniq[i] = other.prilojeniq[i];
- }
- }
- return *this;
- }
- template <typename T>
- Device<T>::~Device()
- {
- delete[] marka;
- delete[] prilojeniq;
- }
- template <typename T>
- class Phone : public Device<T>
- {
- private :
- double price;
- public :
- Phone(double = 0,const char* = " ",int = 0);
- void infoButon() const;
- bool CompareWith(const Phone<T>&);
- bool operator>(const Phone<T>&);
- void print() const;
- };
- template <typename T>
- void Phone<T>::print() const
- {
- cout << price << " " << marka << " " << count << endl;
- }
- template <typename T>
- bool Phone<T>::operator>(const Phone<T>& phone)
- {
- return this->price > phone.price && this->count > phone.count;
- }
- template <typename T>
- bool Phone<T>::CompareWith(const Phone<T>& phone)
- {
- return *this > phone;
- }
- template <typename T>
- Phone<T>::Phone(double price,const char* marka,int count) : Device<T>(marka,count)
- {
- this->price = price;
- }
- template <typename T>
- void Phone<T>::infoButon() const
- {
- Device<T>::print();
- }
- template <typename T>
- class Catalog
- {
- private :
- Phone<T>* phones;
- int count;
- public :
- Catalog(int = 0);
- Catalog(const Catalog<T>&);
- Catalog<T>& operator=(const Catalog<T>&);
- ~Catalog();
- void addPhones(const Phone<T>&);
- void print() const;
- };
- template <typename T>
- void Catalog<T>::print() const
- {
- for(int i = 0;i < count;i++)
- {
- phones[i].print();
- }
- }
- template <typename T>
- void Catalog<T>::addPhones(const Phone<T>& phone)
- {
- phones[count++] = phone;
- }
- template <typename T>
- Catalog<T>::Catalog(int count)
- {
- this->count = count;
- phones = new Phone<T>[count];
- }
- template <typename T>
- Catalog<T>::Catalog(const Catalog<T>& other)
- {
- this->count = count;
- for(int i = 0;i < count;i++)
- {
- this->phones[i] = other.phones[i];
- }
- }
- template <typename T>
- Catalog<T>& Catalog<T>::operator=(const Catalog<T>& other)
- {
- if(this != &other)
- {
- delete[] phones;
- this->count = count;
- for(int i = 0;i < count;i++)
- {
- phones[i] = other.phones[i];
- }
- }
- return *this;
- }
- template <typename T>
- Catalog<T>::~Catalog()
- {
- delete[] phones;
- }
- int main()
- {
- Catalog<double> catalog(5);
- Phone<double> a(1200.60,"LG",15);
- Phone<double> b(999.99,"Samsung",6);
- Phone<double> c(1800.99,"Lenovo",20);
- Phone<double> d(2000.99,"Huawei",18);
- catalog.addPhones(a);
- catalog.addPhones(b);
- catalog.addPhones(c);
- catalog.addPhones(d);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement