Toliak

lab1 dump 2019.02.07 13:30

Feb 7th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <utility>
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. class CBaseEntity
  7. {
  8. private:
  9.     std::string name;
  10.     unsigned long days = 0;
  11.     unsigned long price = 0;
  12.  
  13. public:
  14.     // Default constructor
  15.     CBaseEntity() = default;
  16.     CBaseEntity(const CBaseEntity&) = default;
  17.     CBaseEntity(CBaseEntity&&) = default;
  18.  
  19.     // Data-based constructor
  20.     CBaseEntity(std::string name, unsigned long days, unsigned long price)
  21.         : name(std::move(name)), days(days), price(price)
  22.     {}
  23.  
  24.     ~CBaseEntity() = default;
  25.  
  26.     // Total price getter
  27.     unsigned long getTotalPrice() const noexcept
  28.     {
  29.         return days * price;
  30.     }
  31.  
  32.     // Name getter
  33.     const std::string &getName() const
  34.     {
  35.         return name;
  36.     }
  37.  
  38.     // Name setter
  39.     void setName(const std::string &name)
  40.     {
  41.         this->name = name;
  42.     }
  43.  
  44.     // Days getter
  45.     unsigned long getDays() const
  46.     {
  47.         return days;
  48.     }
  49.  
  50.     // Days setter
  51.     void setDays(unsigned long days)
  52.     {
  53.         this->days = days;
  54.     }
  55.  
  56.     // Price getter
  57.     unsigned long getPrice() const
  58.     {
  59.         return price;
  60.     }
  61.  
  62.     // Price setter
  63.     void setPrice(unsigned long price)
  64.     {
  65.         this->price = price;
  66.     }
  67.  
  68.     // Copy operator overloading
  69.     CBaseEntity &operator=(const CBaseEntity &) = default;
  70.  
  71.     // Input, output operator overloading
  72.     friend std::ostream &operator<<(std::ostream &stream, const CBaseEntity &entity);
  73.  
  74. };
  75.  
  76. std::ostream &operator<<(std::ostream &stream, const CBaseEntity &entity)
  77. {
  78.     stream << "\tName: " << entity.name << std::endl;
  79.     stream << "\tDays: " << entity.days << std::endl;
  80.     stream << "\tPrice for day: " << entity.price << std::endl;
  81.     stream << "\tTotal price: " << entity.getTotalPrice() << std::endl;
  82.  
  83.     return stream;
  84. }
  85.  
  86. int main()
  87. {
  88.     size_t arraySize;
  89.     std::cout << "Enter array size: ";
  90.     std::cin >> arraySize;
  91.     auto array = new CBaseEntity[arraySize];
  92.  
  93.     // Input all entities
  94.     for (size_t i = 0; i <arraySize; i++) {
  95.         std::string name;
  96.         unsigned long days;
  97.         unsigned long price;
  98.  
  99.         std::cout << "Entity index: " << i << std::endl;
  100.  
  101.         std::cout << "\tName: ";
  102.         std::cin.ignore();
  103.         std::getline(std::cin, name);
  104.  
  105.         std::cout << "\tDays: ";
  106.         std::cin >> days;
  107.  
  108.         std::cout << "\tPrice for day: ";
  109.         std::cin >> price;
  110.  
  111.         array[i] = CBaseEntity(name, days, price);
  112.     }
  113.  
  114.     // Output
  115.     for (size_t i = 0; i <arraySize; i++) {
  116.         std::cout << "Entity index: " << i << std::endl;
  117.         std::cout << array[i] << std::endl;
  118.     }
  119.  
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment