-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathmattemplate.cpp
69 lines (62 loc) · 1.54 KB
/
mattemplate.cpp
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
#include <iostream>
using namespace std;
// Class Template
template<typename T>
class Mat
{
size_t rows;
size_t cols;
T * data;
public:
Mat(size_t rows, size_t cols): rows(rows), cols(cols)
{
data = new T[rows * cols]{};
}
~Mat()
{
delete [] data;
}
Mat(const Mat&) = delete;
Mat& operator=(const Mat&) = delete;
T getElement(size_t r, size_t c);
bool setElement(size_t r, size_t c, T value);
};
template <typename T>
T Mat<T>::getElement(size_t r, size_t c)
{
if ( r >= this->rows || c >= this->cols)
{
cerr << "getElement(): Indices are out of range" << endl;
return 0;
}
return data[ this->cols * r + c];
}
template <typename T>
bool Mat<T>::setElement(size_t r, size_t c, T value)
{
if ( r >= this->rows || c >= this->cols)
{
cerr << "setElement(): Indices are out of range" << endl;
return false;
}
data[ this->cols * r + c] = value;
return true;
}
template class Mat<int>; // Explicitly instantiate template Mat<int>
//template Mat<float> and Mat<double> will be instantiate implicitly
int main()
{
Mat<int> imat(3,4);
imat.setElement(1, 2, 256);
Mat<float> fmat(2,3);
fmat.setElement(1, 2, 3.14159f);
Mat<double> dmat(2,3);
dmat.setElement(1, 2, 2.718281828);
// Mat<float> fmat2(fmat); //error
// Mat<float> fmat3(2,3);
// fmat3 = fmat; //error
cout << imat.getElement(1,2) << endl;
cout << fmat.getElement(1,2) << endl;
cout << dmat.getElement(1,2) << endl;
return 0;
}