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