-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSafeArray.cpp
80 lines (68 loc) · 1.27 KB
/
SafeArray.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
//Ryan Ramphal
//CS211
//HW#7
#include "SafeArray.h"
#include <iostream>
using namespace std;
template <class T>
SafeArray<T>::SafeArray()
{
size = 0;
array = NULL;
}
template <class T>
SafeArray<T>::SafeArray(int s)
{
size = s;
array = new T[size];
}
template <class T>
int SafeArray<T>::length() const
{
return size;
}
template <class T>
T& SafeArray<T>::operator [] (int index)
{
if (index < 0 || index >= size)
{
cerr << "Index: " << index << " is out of bounds." << endl;
system("PAUSE");
exit(1);
}
return array[index];
}
template <class T>
SafeArray<T>::~SafeArray()
{
if (array != NULL)
delete[] array;
}
template <class T>
SafeArray<T>& SafeArray<T>::operator = (const SafeArray<T>& other)
{
if (this != &other)
{
if (array != NULL)
{
delete[] array;
}
size = other.size;
array = new T[size];
for (int i = 0; i < size; i++)
{
array[i] = other.array[i];
}
}
return *this;
}
template <class T>
SafeArray<T>::SafeArray(const SafeArray<T>& other)
{
size = other.size;
array = new T[size];
for (int i = 0; i < size; i++)
{
array[i] = other.array[i];
}
}