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