Advertisement
avr39-ripe

PV913Destructors

May 13th, 2020
1,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class DynArray
  4. {
  5.     int* arr;
  6.     int size;
  7. public:
  8.     DynArray(int pSize) : arr{ new int[pSize] {} }, size{ pSize } {std::cout << "DynArr constructed for " << size << " elements!\n"; };
  9.     int getElem(int idx) { return arr[idx]; };
  10.     void setElem(int idx, int val) { arr[idx] = val; };
  11.     void print();
  12.     ~DynArray() { if (arr) { delete[] arr; }; std::cout << "DynArr destructed for " << size << " elements!\n"; };
  13. };
  14.  
  15. void DynArray::print()
  16. {
  17.     for (int i{ 0 }; i < size; ++i)
  18.     {
  19.         std::cout << arr[i] << ' ';
  20.     }
  21.     std::cout << '\n';
  22. }
  23.  
  24. void testFun()
  25. {
  26.     DynArray funMas{ 11 };
  27.     funMas.print();
  28.     funMas.setElem(6, 867);
  29.     funMas.print();
  30. }
  31.  
  32. int main()
  33. {
  34.     //int x{ 5 };
  35.     //x = 25;
  36.     //std::cout << x << '\n';
  37.  
  38.  
  39.     //int* y{ new int{6} };
  40.     //*y = 36;
  41.     //std::cout << *y << '\n';
  42.     //delete y;
  43.  
  44.     DynArray arr1{ 8 };
  45.     arr1.print();
  46.     arr1.setElem(5, 87);
  47.     arr1.print();
  48.  
  49.  
  50.     //DynArray* mas{ new DynArray{ 5 } };
  51.     //(*mas).print();
  52.     //(*mas).setElem(3, 55);
  53.     //(*mas).print();
  54.     //delete mas;
  55.  
  56.     std::cout << "Pre testFun\n";
  57.     testFun();
  58.     std::cout << "Post testFun\n";
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement