Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Array {
- public :
- int GetAt(int index);
- //void reallocate();
- void PushBack(int a);
- void Initialize();
- void Deinitialize();
- private :
- int size;
- int allocatedSize;
- int* buffer;
- void reallocate();
- };
- int Array::GetAt(int index) // возвращает элемент по номеру
- {
- return buffer[index];
- }
- void Array::reallocate()
- {
- int* oldBuffer = buffer;
- int newAllocatedSize = 2 * allocatedSize;
- buffer = new int[newAllocatedSize];
- for (int i = 0; i < size; i++){
- buffer[i] = oldBuffer[i];
- }
- allocatedSize = newAllocatedSize;
- delete[] oldBuffer;
- }
- void Array::PushBack(int a) // добавить элемент в конец массива
- {
- if (size == allocatedSize) {
- reallocate();
- }
- buffer[size] = a;
- size++;
- }
- void Array :: Initialize()
- {
- size = 0;
- allocatedSize = 1;
- buffer = new int[allocatedSize];
- }
- void Array::Deinitialize()
- {
- delete[] buffer;
- allocatedSize = 0;
- size;
- }
- int main()
- {
- Array ar;
- ar.Initialize();
- for (int i = 0; i < 10; i++) {
- ar.PushBack(i);
- }
- //ar.allocatedSize = 10; - нельзя, тк private
- int x = ar.GetAt(4);
- _CrtDumpMemoryLeaks();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement