avr39ripe

cppOvercoatGittAndryiVersion

Jul 29th, 2021
658
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Задание 1.
  2. Создать класс Overcoat (верхняя одежда).
  3. Реализовать перегруженные операторы:
  4. 1. Проверка на равенство типов одежды (операция = =).
  5. 2. Операцию присваивания одного объекта в другой
  6. (операция =).
  7. 3. Сравнение по цене двух пальто одного типа (операция>).
  8.  
  9. #include <iostream>
  10.  
  11. class Overcoat
  12. {
  13.     char* type;
  14.     char* size;
  15.     char* color;
  16.     double price;
  17. public:
  18.     Overcoat(const char* typeP, const char* sizeP, const char* colorP, double priceP);
  19.     Overcoat() : type{ nullptr }, size{ nullptr }, color{ nullptr }, price{ 0 }{}
  20.     Overcoat(const Overcoat& object);
  21.     Overcoat(Overcoat&& object);
  22.  
  23.     Overcoat& setType(const char* typeP);
  24.     Overcoat& setSize(const char* sizeP);
  25.     Overcoat& setColor(const char* colorP);
  26.     Overcoat& setPrise(double priceP) { price = priceP; return *this; };
  27.  
  28.     const char* getType()const { return type; }
  29.     const char* getSize()const { return size; }
  30.     const char* getColor()const { return color; }
  31.     double getPrice()const { return price; }
  32.  
  33.     Overcoat& operator=(const Overcoat& object);
  34.     Overcoat& operator=(Overcoat&& object);
  35.  
  36.     friend bool operator==(const Overcoat& left, const Overcoat& right);
  37.     friend bool operator>(const Overcoat& left, const Overcoat& right);
  38.    
  39.     friend std::ostream& operator<<(std::ostream& out, const Overcoat& object);
  40.  
  41.     ~Overcoat() { delete[] type; delete[] size; delete[] color; }
  42. };
  43.  
  44. Overcoat::Overcoat(const char* typeP, const char* sizeP, const char* colorP, double priceP) :
  45.     type{ new char[strlen(typeP) + 1] }, size{ new char[strlen(sizeP) + 1] }, color{ new char[strlen(colorP) + 1] }, price{ priceP }
  46. {
  47.     strcpy_s(type, strlen(typeP) + 1, typeP);
  48.     strcpy_s(size, strlen(sizeP) + 1, sizeP);
  49.     strcpy_s(color, strlen(colorP) + 1, colorP);
  50. }
  51. Overcoat::Overcoat(const Overcoat& object) :
  52.     type{ new char[strlen(object.type) + 1] }, size{ new char[strlen(object.size) + 1] }, color{ new char[strlen(object.color) + 1] }, price{ object.price }
  53. {
  54.     strcpy_s(type, strlen(object.type) + 1, object.type);
  55.     strcpy_s(size, strlen(object.size) + 1, object.size);
  56.     strcpy_s(color, strlen(object.color) + 1, object.color);
  57. }
  58. Overcoat::Overcoat(Overcoat&& object) :
  59.     type{ object.type }, size{ object.size }, color{ object.color }, price{ object.price }
  60. {
  61.     object.type = nullptr;
  62.     object.size = nullptr;
  63.     object.color = nullptr;
  64.     object.price = 0;
  65. }
  66.  
  67. Overcoat& Overcoat::setType(const char* typeP)
  68. {
  69.     delete[] type;
  70.     type = new char[strlen(typeP) + 1];
  71.     strcpy_s(type, strlen(typeP) + 1, typeP);
  72.     return *this;
  73. }
  74. Overcoat& Overcoat::setSize(const char* sizeP)
  75. {
  76.     delete[] size;
  77.     size = new char[strlen(sizeP) + 1];
  78.     strcpy_s(size, strlen(sizeP) + 1, sizeP);
  79.     return *this;
  80. }
  81. Overcoat& Overcoat::setColor(const char* colorP)
  82. {
  83.     delete[] color;
  84.     color = new char[strlen(colorP) + 1];
  85.     strcpy_s(color, strlen(colorP) + 1, colorP);
  86.     return *this;
  87. }
  88.  
  89. Overcoat& Overcoat::operator=(const Overcoat& object)
  90. {
  91.     if (this == &object) { return *this; }
  92.    
  93.     delete[] type;
  94.     delete[] size;
  95.     delete[] color;
  96.  
  97.     type = new char[strlen(object.type) + 1];
  98.     size = new char[strlen(object.size) + 1];
  99.     color = new char[strlen(object.color) + 1];
  100.     strcpy_s(type, strlen(object.type) + 1, object.type);
  101.     strcpy_s(size, strlen(object.size) + 1, object.size);
  102.     strcpy_s(color, strlen(object.color) + 1, object.color);
  103.     price = object.price;
  104.     return *this;
  105. }
  106. Overcoat& Overcoat::operator=(Overcoat&& object)
  107. {
  108.     if (this == &object) { return *this; }
  109.  
  110.     delete[] type;
  111.     delete[] size;
  112.     delete[] color;
  113.  
  114.     type = object.type;
  115.     size = object.size;
  116.     color = object.color;
  117.     price = object.price;
  118.  
  119.     object.type = nullptr;
  120.     object.size = nullptr;
  121.     object.color = nullptr;
  122.     object.price = 0;
  123.  
  124.     return *this;
  125. }
  126.  
  127. bool operator==(const Overcoat& left, const Overcoat& right)
  128. {
  129.     return (strcmp(left.type, right.type) == 0 and strcmp(left.size, right.size) == 0 and strcmp(left.color, right.color) == 0 and left.price == right.price );
  130. }
  131.  
  132. bool operator>(const Overcoat& left, const Overcoat& right)
  133. {
  134.     return (left.price > right.price);
  135. }
  136.  
  137. std::ostream& operator<<(std::ostream& out, const Overcoat& object)
  138. {
  139.     if (object.type and object.size and object.color and object.price)
  140.     {
  141.         out << "Type: " << object.type << " Size: " << object.size << " Color: " << object.color << " Price: " << object.price << '\n';
  142.     }
  143.     else { out << "[empty overcoat]"; }
  144.     return out;
  145. }
  146.  
  147. int main()
  148. {
  149.     Overcoat overcoat_1;
  150.     std::cout << overcoat_1 << '\n';
  151.  
  152.     Overcoat coatBlack{ "coat", "M", "black", 3500.5 };
  153.     Overcoat coatBrown{ "coat", "L", "brown", 4850.8 };
  154.     std::cout << coatBlack << coatBrown << '\n';
  155.  
  156.     overcoat_1 = coatBlack;
  157.     Overcoat coat_2{ std::move(coatBrown) };
  158.     std::cout << coatBlack << overcoat_1 << coat_2 << coatBrown << '\n';
  159.  
  160.     Overcoat leatherJacket{ "jacket", "M", "black", 5700 };
  161.     std::cout << std::boolalpha << "\nJacket is more expensive than a coat #2 - " << (leatherJacket > coat_2) << '\n';
  162.    
  163.     std::cout << std::boolalpha << "\nCoats black and #1 are the same - " << (coatBlack == overcoat_1) << '\n';
  164.     std::cout << std::boolalpha << "\nCoats black and #2 are the same - " << (coatBlack == coat_2) << '\n';
  165.  
  166.     return 0;
  167. }
  168.  
  169.  
RAW Paste Data