Skip to content

Commit 2bff64b

Browse files
committed
implement more member functions
1 parent 3bcfe5e commit 2bff64b

File tree

4 files changed

+155
-16
lines changed

4 files changed

+155
-16
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.git/
2-
.vscode/
2+
.vscode/
3+
*.gch
4+
*.o

ArrayList.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,29 @@ ArrayList::ArrayList(ArrayList&& arrayToMove)
3232
// getters
3333
int ArrayList::getCapacity() const
3434
{
35-
return capacity;
35+
return this->capacity;
3636
}
3737

3838
int ArrayList::getUsed() const
3939
{
40-
return used;
40+
return this->used;
4141
}
4242

4343
int *ArrayList::getPArray() const
4444
{
45-
return pArray;
45+
return this->pArray;
4646
}
4747

48+
std::ostream &operator<<(std::ostream &output, const IntList &someList)
49+
{
50+
//base case if used is 0, then we have a fresh/empty array; return empty string
51+
if (someList.getUsed() == 0)
52+
{
53+
return output;
54+
}
55+
for (int i = 0; i < someList.getUsed(); i++)
56+
{
57+
output << someList.dynarray[i] << " ";
58+
}
59+
return output;
60+
}

ArrayList.h

+136-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include<iostream>
22
#include<string>
3+
34
using namespace std;
45

56
class ArrayList
@@ -10,16 +11,139 @@ class ArrayList
1011
int* pArray; // pointer to a dynamically allocated array of integers
1112

1213
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+
}
25149
};

ArrayListDriver

55.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)