-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.cpp
More file actions
98 lines (84 loc) · 1.64 KB
/
Array.cpp
File metadata and controls
98 lines (84 loc) · 1.64 KB
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
#include "Array.h"
Array::Array (void)
: data_ (new int[0]),
sizeArr_ (0)
{
//Nothing here. Won't be using
}
Array::Array (const int size)
: data_ (new int[size]),
sizeArr_ (size)
{
//Nothing here
}
Array::Array (const Array & arr)
: data_ (new int[arr.size ()]),
sizeArr_ (arr.size ())
{
for (int i = 0; i < this->sizeArr_; i++) {
int *thisLocation = &this->data_[i];
int *arrayLocation = &arr.data_[i];
*thisLocation = *arrayLocation;
}
}
Array::~Array (void)
{
//delete Array allocation
delete [] this->data_;
}
int Array::size (void) const
{
return this->sizeArr_;
}
void Array::setSize (int size)
{
this->sizeArr_ = size;
}
const Array & Array::operator = (const Array & rhs)
{
if (&(*this) == &(rhs)) {
return *this;
} else {
this->setSize (rhs.size ());
for (int i = 0; i < this->sizeArr_; i++) {
int thisLocation = this->data_[i];
int rhsLocation = rhs.data_[i];
thisLocation = rhsLocation;
}
return *this;
}// end else
}
int & Array::operator [] (int index)
{
return this->data_[index];
}
const int & Array::operator [] (int index) const
{
return this->data_[index];
}
void Array::printArr (void)
{
for (int i; i < this->size (); i++) {
std::cout << "[" << i << "]: " << data_[i] << std::endl;
}
}
void Array::generateArray (int size)
{
for (int i = 0; i < this->size (); i++) {
this->set (i, rand()%size);
}
}
void Array::set (int index, int element)
{
this->data_[index] = element;
}
int Array::get (int index)
{
return this->data_[index];
}
void Array::fill (int element)
{
for (int i = 0; i < this->sizeArr_; i++) {
this->data_[i] = element;
}
}