-
Notifications
You must be signed in to change notification settings - Fork 1
/
Array.cpp
75 lines (61 loc) · 1.4 KB
/
Array.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
#include "Kingdom.h"
template <class T>
Array<T>::Array( int index = DEFAULT_SIZE ):
size(index)
{
arr = new T[index];
for ( int i = 0; i < index; i++ ) {
arr[i] = (T) 0; // cast 0 to type of array and store it
}
type = 0;
}
template <class T>
Array<T>::Array( const Array& r_warriorArr )
{
size = r_warriorArr.getSize();
arr = new T[ size ];
for ( int i = 0; i < size; i++ ) {
arr[i] = r_warriorArr[i];
}
type = w_warriorArr[0].getWarriorPosition();
}
template <class T>
Array<T>::~Array() {
delete [] arr;
}
template <class T>
T& Array<T>::operator[] ( int index ) {
return arr[index];
}
template <class T>
const T& Array<T>::operator[] ( int index ) const {
return arr[index];
}
template <class T>
Array<T>& Array<T>::operator= ( const Array& r_warriorArr ) {
if ( r_warriorArr == this ) {
return *this;
}
delete [] arr;
size = r_warriorArr.getSize();
arr = new T[size];
for ( int i = 0; i < size; i++ ) {
arr[i] = r_warriorArr[i];
}
return *this;
}
template <class T>
int Array<T>::getSize() const {
return size;
}
template <class T>
int Array<T>::getType() const {
return type;
}
template <class T> // not defined in Array class so no need for Array::operator<< syntax
ostream& operator<< ( ostream& output, const Array<T>& r_array ) {
for ( int i = 0; i < r_array.getSize(); i++ ) {
output << r_array[i]; // r_array[i] can be used since we overrode operator[]
}
return output;
}