Advertisement
Ifrail

Less9 Task2

Jan 23rd, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. /*Объекты и указатели - 2*/
  2.  
  3. /*
  4.     Дополните класс SimpleVector из предыдущей задачи, методом refill, который принимает другой экземпляр SimpleVector и заполняет принимающий объект данными из переданного.
  5. Дополнительно, конструктор копирования должен вывести "memory allocate from copy"
  6. */
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. class SimpleVector {
  14.     int* _data;
  15.     int _len;
  16.  
  17. public:
  18.     SimpleVector(int len) {
  19.         cout << "memory allocate" << endl;
  20.         _len = len;
  21.         _data = new int[_len];
  22.     }
  23.  
  24.     SimpleVector(const SimpleVector& sv) {
  25.         cout << "memory allocate from copy" << endl;
  26.         _len = sv._len;
  27.         _data = new int[_len];
  28.         for (int i = 0; i < _len; i++)
  29.             _data[i] = sv._data[i];
  30.  
  31.     }
  32.  
  33.     ~SimpleVector() {
  34.         if (_len != 0) {
  35.             delete[] _data;
  36.             _len = 0;
  37.             cout << "memory clear" << endl;
  38.         }
  39.     }
  40.  
  41.     void set(int idx, int val) {
  42.         _data[idx] = val;
  43.     }
  44.  
  45.     void print() {
  46.         cout << _data[0];
  47.         for (int i = 1; i < _len; i++) {
  48.             cout << " " << _data[i];
  49.         }
  50.         cout << endl;
  51.     }
  52.  
  53.     void reverse() {
  54.         for (int i = 0; i < _len / 2; i++) {
  55.             swap(_data[i], _data[_len - i - 1]);
  56.         }
  57.     }
  58.  
  59.     void read() {
  60.         for (int i = 0; i < _len; i++) {
  61.             int x;
  62.             cin >> x;
  63.             set(i, x);
  64.         }
  65.     }
  66.  
  67.     // сделать копию переданного массива
  68.     void refill(SimpleVector sv) {
  69.         _len = sv._len;
  70.         delete[] _data;
  71.  
  72.         _data = new int[_len];
  73.         for (int i = 0; i < _len; i++) {
  74.             _data[i] = sv._data[i];
  75.         }
  76.     }
  77. };
  78.  
  79.  
  80.  
  81. int main() {
  82.     int len;
  83.     cin >> len;
  84.     SimpleVector sv1(len);
  85.     sv1.read();
  86.  
  87.     SimpleVector sv2(len);
  88.     sv2.read();
  89.    
  90.     sv2.refill(sv1);
  91.  
  92.     sv1.print();
  93.     sv2.print();
  94. }
  95.  
  96. /*
  97. Тест 1
  98. Ввод:
  99. 7
  100. 7 7 7 0 0 0 9
  101. 12
  102. 8 1 8 3 8 5 6 7 3 4 5 6
  103.  
  104. Вывод:
  105. memory allocate
  106. memory allocate
  107. memory allocate from copy
  108. memory clear
  109. 7 7 7 0 0 0 9
  110. 7 7 7 0 0 0 9
  111. memory clear
  112. memory clear
  113.  
  114. Тест 2
  115. Ввод:
  116. 15
  117. 7 0 4 5 9 3 4 9 5 2 2 4 6 3 2
  118. 5
  119. 4 5 6 7 1
  120. Вывод:
  121. memory allocate
  122. memory allocate
  123. memory allocate from copy
  124. memory clear
  125. 7 0 4 5 9 3 4 9 5 2 2 4 6 3 2
  126. 7 0 4 5 9 3 4 9 5 2 2 4 6 3 2
  127. memory clear
  128. memory clear
  129. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement