-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_with_templates.cpp
101 lines (87 loc) · 1.64 KB
/
matrix_with_templates.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <stdexcept>
using namespace std;
template <unsigned int R, unsigned int C, typename _T = int>
class matrix
{
public:
matrix()
{
mat = new _T*[R];
for(unsigned int i = 0;i < R;i++)
{
mat[i] = new _T[C];
}
}
_T* operator[](int index)
{
if(index >= R)
{
throw std::out_of_range("Index out of range");
}
return mat[index];
}
const _T* operator[](int index) const
{
if(index >= R)
{
throw std::out_of_range("Index out of range");
}
return mat[index];
}
private:
_T** mat;
};
template <unsigned int R, unsigned int C, typename _T>
matrix<R, C, _T> operator+(const matrix<R, C, _T>& m1, const matrix<R, C, _T>& m2)
{
matrix<R, C, _T> answer;
for(unsigned int i = 0;i < R;i++)
{
for(unsigned int j = 0;j < C;j++)
{
answer[i][j] = m1[i][j] + m2[i][j];
}
}
return answer;
}
template <unsigned int R, unsigned int C, typename _T>
std::ostream& operator<<(std::ostream& os, const matrix<R, C, _T>& m)
{
for(unsigned int i = 0;i < R;i++)
{
for(unsigned int j = 0;j < C;j++)
{
os<<m[i][j]<<" ";
}
os<<std::endl;
}
return os;
}
template <unsigned int R, unsigned int C, typename _T>
std::istream& operator>>(std::istream& is, matrix<R, C, _T>& m)
{
for(unsigned int i = 0;i < R;i++)
{
for(unsigned int j = 0;j < C;j++)
{
is>>m[i][j];
}
}
return is;
}
int main()
{
matrix<3,3> m1;
cin>>m1;
matrix<3,3> m2;
cin>>m2;
cout<<m1 + m2; // perfectly valid as both are matrix<3,3>
matrix<4,4> m3;
cin>>m3;
// cout<<m1 + m3; // error because m1 is of type matrix<3,3> where as m3 is of typye matrix<4,4>
#ifdef WIN32
system("PAUSE");
#endif // WIN32
return 0;
}