Advertisement
bogolyubskiyalexey

kaspersky problem #1: cpp

Jul 15th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. // https://habr.com/article/353768/
  2. // problem #1
  3. #include <string>
  4. #include <memory>
  5. #include <iostream>
  6. #include <iomanip>
  7. class Item
  8. {
  9. public:
  10.     virtual ~Item() { }
  11.     Item(const char* data): m_data(std::make_unique<std::string>(data)) {  }
  12.     Item(Item&& obj): m_data(std::move(obj.m_data)) { }
  13.    
  14.     virtual const std::string GetContent() const { return m_data ? *m_data : ""; }
  15. private:
  16.     std::unique_ptr<std::string> m_data;
  17. };
  18. class ItemEx : public Item
  19. {
  20. public:
  21.     ItemEx(const char* data, const char* dataEx): Item(data), m_dataEx(std::make_unique<std::string>(dataEx)) {  }
  22.    
  23.     ItemEx(ItemEx&& obj)
  24.         : Item(std::move(obj))
  25.         , m_dataEx(std::move(obj.m_dataEx))
  26.     { }
  27.  
  28.     virtual const std::string GetContent() const override { return Item::GetContent() + (m_dataEx ? *m_dataEx : ""); }
  29. private:
  30.     std::unique_ptr<std::string> m_dataEx;
  31. };
  32. void Func()
  33. {
  34.     ItemEx item("123", "456");
  35.     std::cout << item.GetContent() << std::endl;
  36.     ItemEx newItem = std::move(item);
  37.     std::cout << newItem.GetContent() << std::endl;
  38.     std::cout << item.GetContent() << std::endl;
  39. }
  40. int main()
  41. {
  42.     Func();
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement