1
1
#include < iostream>
2
2
#include < string>
3
+
3
4
using namespace std ;
4
5
5
6
class ArrayList
@@ -10,16 +11,139 @@ class ArrayList
10
11
int * pArray; // pointer to a dynamically allocated array of integers
11
12
12
13
public:
13
- // constructors
14
- ArrayList (); // default constructor
15
- ArrayList (const ArrayList &arrayToCopy); // copy constructor
16
- ArrayList (ArrayList&& arrayToMove); // move constructor
17
-
18
- // getters
19
- int getCapacity () const ;
20
- int getUsed () const ;
21
- int *getPArray () const ;
22
-
23
- // operator overload
24
- friend ostream &operator <<(ostream &sout, const ArrayList &someList);
14
+ // default constructor
15
+ ArrayList ()
16
+ {
17
+ cout << " default constructor is called" << endl;
18
+ capacity = 1 ;
19
+ used = 0 ;
20
+ pArray = new int [capacity];
21
+ }
22
+
23
+ // copy constructor
24
+ ArrayList (const ArrayList &arrayToCopy)
25
+ {
26
+ cout << " copy constructor is called" << endl;
27
+ capacity = arrayToCopy.getCapacity ();
28
+ used = arrayToCopy.size ();
29
+ pArray = new int [capacity];
30
+
31
+ for (int i = 0 ; i < capacity; i++)
32
+ {
33
+ pArray[i] = arrayToCopy.getPArray ()[i];
34
+ }
35
+ }
36
+
37
+ // move constructor
38
+ ArrayList (ArrayList&& arrayToMove)
39
+ {
40
+ cout << " move constructor is called" << endl;
41
+ capacity = arrayToMove.capacity ;
42
+ used = arrayToMove.used ;
43
+ pArray = arrayToMove.pArray ;
44
+ arrayToMove.capacity = 0 ;
45
+ arrayToMove.used = 0 ;
46
+ arrayToMove.pArray = nullptr ;
47
+ }
48
+
49
+ // copy assignment operator
50
+ ArrayList& operator =(const ArrayList &rhs)
51
+ {
52
+ cout << " copy assignment operator is called" << endl;
53
+ // if the addresses are not the same, proceed with copying
54
+ if (&rhs != this )
55
+ {
56
+ capacity = rhs.getCapacity ();
57
+ used = rhs.size ();
58
+ delete[] pArray;
59
+ pArray = new int [capacity];
60
+
61
+ for (int i = 0 ; i < capacity; i++)
62
+ {
63
+ pArray[i] = rhs.getPArray ()[i];
64
+ }
65
+ }
66
+ return *this ;
67
+ }
68
+
69
+ // move assignment operator
70
+ ArrayList& operator =(ArrayList &&rhs)
71
+ {
72
+ cout << " move assignment operator is called" << endl;
73
+ if (&rhs != this )
74
+ {
75
+ delete[] pArray;
76
+ capacity = rhs.capacity ;
77
+ used = rhs.used ;
78
+ pArray = rhs.pArray ;
79
+ rhs.pArray = nullptr ;
80
+ }
81
+ return *this ;
82
+ }
83
+
84
+ // destructor
85
+ ~ArrayList ()
86
+ {
87
+ delete[] pArray;
88
+ }
89
+
90
+ // determines whether used equals zero
91
+ bool empty () const
92
+ {
93
+ return used == 0 ;
94
+ }
95
+
96
+ // determines whether used equals capacity
97
+ bool full () const
98
+ {
99
+ return used == capacity;
100
+ }
101
+
102
+ // getters for all member variables
103
+ int getCapacity () const
104
+ {
105
+ return capacity;
106
+ }
107
+
108
+ int *getPArray () const
109
+ {
110
+ return pArray;
111
+ }
112
+
113
+ int size () const
114
+ {
115
+ return used;
116
+ }
117
+
118
+ // double the capacity of pArray
119
+ void resize
120
+ {
121
+ int newCapacity = this ->capacity * 2 ;
122
+ int * newPArray = new int [newCapacity];
123
+
124
+ for (int i = 0 ; i < this ->capacity ; i++)
125
+ {
126
+ newPArray[i] = this ->pArray [i];
127
+ }
128
+ this ->capacity = newCapacity;
129
+ delete[] this ->pArray ;
130
+ this ->pArray = newParray;
131
+ }
132
+
133
+
134
+
135
+ // << operator overload
136
+ friend ostream &operator <<(ostream &output, const ArrayList &listToPrint)
137
+ {
138
+ // if used is 0, return empty string because we can't print anything
139
+ if (listToPrint.size () == 0 )
140
+ {
141
+ return output;
142
+ }
143
+ for (int i = 0 ; i < listToPrint.size (); i++)
144
+ {
145
+ output << listToPrint.pArray [i] << " " ;
146
+ }
147
+ return output;
148
+ }
25
149
};
0 commit comments