-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraytp.h
50 lines (44 loc) · 851 Bytes
/
arraytp.h
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
// arraytp.h -- Array Template
#ifndef ARRAYTP_H_
#define ARRAYTP_H_
#include <iostream>
#include <cstdlib>
template <class T, int n>
class ArrayTP
{
private:
T ar[n];
public:
ArrayTP() {};
explicit ArrayTP (const T & v);
virtual T & operator[] (int i);
virtual T operator[] (int i) const;
};
template <class T, int n>
ArrayTP<T, n>::ArrayTP (const T & v)
{
for (int i = 0; i < n; i++) {
ar[i] = v;
}
}
template <class T, int n>
T & ArrayTP<T, n>::operator[] (int i)
{
if (i < 0 || i >= n) {
std::cerr << "Error in array limits: " << i
<< " is out of range\n";
std::exit (EXIT_FAILURE);
}
return ar[i];
}
template <class T, int n>
T ArrayTP<T, n>::operator[] (int i) const
{
if (i < 0 || i >= n) {
std::cerr << "Error in array limits: " << i
<< "is out of range\n";
std::exit (EXIT_FAILURE);
}
return ar[i];
}
#endif