Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Array {
  8. public :
  9.  
  10. int GetAt(int index);
  11. //void reallocate();
  12. void PushBack(int a);
  13. void Initialize();
  14. void Deinitialize();
  15.  
  16. private :
  17. int size;
  18. int allocatedSize;
  19. int* buffer;
  20. void reallocate();
  21.  
  22. };
  23.  
  24. int Array::GetAt(int index) // возвращает элемент по номеру
  25. {
  26. return buffer[index];
  27. }
  28.  
  29. void Array::reallocate()
  30. {
  31. int* oldBuffer = buffer;
  32. int newAllocatedSize = 2 * allocatedSize;
  33. buffer = new int[newAllocatedSize];
  34. for (int i = 0; i < size; i++){
  35. buffer[i] = oldBuffer[i];
  36. }
  37. allocatedSize = newAllocatedSize;
  38. delete[] oldBuffer;
  39. }
  40.  
  41. void Array::PushBack(int a) // добавить элемент в конец массива
  42. {
  43. if (size == allocatedSize) {
  44. reallocate();
  45. }
  46. buffer[size] = a;
  47. size++;
  48. }
  49. void Array :: Initialize()
  50. {
  51. size = 0;
  52. allocatedSize = 1;
  53. buffer = new int[allocatedSize];
  54. }
  55.  
  56. void Array::Deinitialize()
  57. {
  58. delete[] buffer;
  59. allocatedSize = 0;
  60. size;
  61. }
  62. int main()
  63. {
  64. Array ar;
  65. ar.Initialize();
  66. for (int i = 0; i < 10; i++) {
  67. ar.PushBack(i);
  68. }
  69. //ar.allocatedSize = 10; - нельзя, тк private
  70.  
  71. int x = ar.GetAt(4);
  72. _CrtDumpMemoryLeaks();
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement