-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patharray.cpp
More file actions
72 lines (64 loc) · 949 Bytes
/
array.cpp
File metadata and controls
72 lines (64 loc) · 949 Bytes
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
#include "array.h"
#include <stdexcept>
array::array() :
n(0),
p(nullptr)
{
}
array::array(array&& array) :
p(nullptr), n(0)
{
std::swap(p, array.p);
std::swap(n, array.n);
}
array::array(const array &other) :
n(0),
p(nullptr)
{
if (other.n > 0)
{
n = other.n;
p = new double[n];
for (int i = 0; i < other.n; ++i)
{
p[i] = other.p[i];
}
}
}
array::array(const int size, const double value) :
n(0),
p(nullptr)
{
if (size > 0)
{
n = size;
p = new double[n];
for (int i = 0; i < size; ++i)
{
p[i] = value;
}
}
}
array array::operator=(array ©Array)
{
n = copyArray.n;
p = copyArray.p;
copyArray.n = 0;
copyArray.p = nullptr;
return *this;
}
double array::at(const int index) const
{
if (index >= 0 && index < n)
{
return p[index];
}
else
{
throw std::out_of_range("Index is out of range.");
}
}
int array::size()
{
return n;
}