-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainers.cpp
102 lines (84 loc) · 1.36 KB
/
Containers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Containers.h"
#include <iostream>
using namespace std;
int Container::getSize() const
{
return 0;
}
int Container::getCapacity() const
{
return 0;
}
void Container::show() const
{
}
void Container::addCapacity(int n)
{
}
Container::~Container()
{
}
MyArray::MyArray()
{
ar = new int[0];
}
MyArray::MyArray(int n)
{
ar = new int[size = n] {0};
}
MyArray::MyArray(const MyArray& ar)
{
this->ar = new int[size = ar.size];
for (int i = 0; i < size; i++)
this->ar[i] = ar.ar[i];
}
int MyArray::getSize() const
{
int result = 0;
for (int i = 0; i < getCapacity(); i++)
if (ar[i] != 0)
result = i + 1;
return result;
}
int MyArray::getCapacity() const
{
return size;
}
void MyArray::show() const
{
for (int i = 0; i < getCapacity(); i++)
cout << ar[i]<<" ";
cout << std::endl;
}
void MyArray::addCapacity(int n)
{
int* tmp = new int[size + n];
for (int i = 0; i < size; i++)
tmp[i] = ar[i];
for (int i = size; i < size + n; i++)
tmp[i] = 0;
delete[] ar;
ar = tmp;
size = size + n;
tmp = nullptr;
}
int& MyArray::operator[](int index)
{
return ar[index];
}
MyArray& MyArray::operator=(const MyArray& ar)
{
if (ar.ar == this->ar)
return *this;
delete[]this->ar;
size = ar.size;
this->ar = new int[size];
for (int i = 0; i < size; i++)
this->ar[i] = ar.ar[i];
return *this;
}
MyArray::~MyArray()
{
delete[] ar;
ar = nullptr;
}