-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patharray.cpp
More file actions
82 lines (70 loc) · 1006 Bytes
/
array.cpp
File metadata and controls
82 lines (70 loc) · 1006 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
73
74
75
76
77
78
79
80
81
82
#include <stdexcept>
#include "array.h"
array::array()
{
p = nullptr;
n = 0;
}
array::array(int size, double value)
{
p = new double[size];
for (int i = 0; i < size; ++i)
{
p[i] = value;
}
n = size;
}
void array::copy_impl(const array& other)
{
p = new double[other.n];
for (int i = 0; i < other.n; i++)
{
p[i] = other.p[i];
}
n = other.n;
}
array::array(const array& other)
{
if (this != &other)
{
copy_impl(other);
}
}
array::array(array&& other)
{
p = other.p;
n = other.n;
other.p = nullptr;
other.n = 0;
}
array::~array()
{
delete[] p;
p = nullptr;
n = 0;
}
int array::size() const
{
return n;
}
void check_index_range(int index, int min, int max)
{
if (index < min || index > max)
{
throw std::out_of_range("index is out of range");
}
}
double array::at(int index) const
{
check_index_range(index, 0, n - 1);
return p[index];
}
array& array::operator=(const array& other)
{
if (this != &other)
{
delete[] p;
copy_impl(other);
}
return *this;
}