avr39ripe

cppMarksStorage

Jul 9th, 2021
1,067
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class MarksStorage
  4. {
  5.     const size_t size;
  6.     int* storage;
  7.     static size_t count;
  8. public:
  9.     static const int maxMark{ 12 };
  10.     static const int minMark{ 1 };
  11.  
  12.     MarksStorage(size_t sizeP) : size{ sizeP }, storage{ new int[sizeP] {} } {std::cout << "Acquire memory for marks!\n"; ++count; }
  13.     MarksStorage() : MarksStorage(1) {}
  14.  
  15.     bool setMark(size_t id, int mark)
  16.     {
  17.         if (id < 0 or id > size - 1 or mark < MarksStorage::minMark or mark > MarksStorage::maxMark) { return false; }
  18.         storage[id] = mark;
  19.         return true;
  20.     }
  21.  
  22.     int getMark(size_t id)
  23.     {
  24.         if (id < 0 or id > size - 1) { return 0; }
  25.         return storage[id];
  26.     }
  27.  
  28.     static int getCount() { return count; }
  29.  
  30.     void print()
  31.     {
  32.         // ++size; // ERROR!!
  33.         std::cout << "Marks: ";
  34.         for (int i{ 0 }; i < size; ++i)
  35.         {
  36.             std::cout << storage[i] << ' ';
  37.         }
  38.         std::cout << '\n';
  39.     }
  40.  
  41.     ~MarksStorage() { delete[] storage; std::cout << "Release memory from marks!\n"; --count; }
  42. };
  43.  
  44. size_t MarksStorage::count{ 0 };
  45.  
  46. int main()
  47. {
  48.     std::cout << "MarksStorage count = " << MarksStorage::getCount() << '\n';
  49.  
  50.     {
  51.         MarksStorage ms1{ 4 };
  52.  
  53.         ms1.setMark(0, 11);
  54.         ms1.setMark(1, 7);
  55.         ms1.setMark(2, 9);
  56.         ms1.setMark(3, 10);
  57.  
  58.         ms1.print();
  59.  
  60.         std::cout << "ms1 count = " << ms1.getCount() << '\n';
  61.         std::cout << "MarksStorage count = " << MarksStorage::getCount() << '\n';
  62.  
  63.         MarksStorage ms4{ 10 };
  64.         std::cout << "MarksStorage count = " << MarksStorage::getCount() << '\n';
  65.     }
  66.     std::cout << "MarksStorage count = " << MarksStorage::getCount() << '\n';
  67.  
  68.     MarksStorage ms2;
  69.     ms2.setMark(4, 11);
  70.     ms2.print();
  71.     ms2.setMark(0, 15);
  72.     ms2.print();
  73.     ms2.setMark(0, 9);
  74.     ms2.print();
  75.  
  76.     std::cout << "ms2 count = " << ms2.getCount() << '\n';
  77.     std::cout << "MarksStorage count = " << MarksStorage::getCount() << '\n';
  78.     return 0;
  79. }
RAW Paste Data