-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path重载实例矩阵相加.cpp
64 lines (53 loc) · 1.31 KB
/
重载实例矩阵相加.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
/*
* @Author: your name
* @Date: 2020-03-17 18:04:28
* @LastEditTime: 2020-04-01 11:03:42
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /test/test.cpp
*/
#include <iostream>
constexpr int row = 2;
constexpr int col = 3;
using std::istream;
using std::ostream;
class Matrix {
public:
Matrix() {}
friend istream &operator>>(istream &, Matrix &);
friend ostream &operator<<(ostream &, Matrix &);
Matrix &operator+=(Matrix m) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] += m.matrix[i][j];
}
}
return *this;
}
private:
int matrix[row][col];
};
istream &operator>>(istream &input, Matrix &m) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
input >> m.matrix[i][j];
}
}
return input;
}
ostream &operator<<(ostream &output, Matrix &m) {
for (int i = 0; i < row; i++) {
std::cout << " | ";
for (int j = 0; j < col; j++) {
output << m.matrix[i][j] <<" ";
}
std::cout << " | " <<std::endl;
}
}
Matrix operator+(Matrix a, Matrix b) { return a += b; }
int main() {
Matrix a, b, c;
std::cin >> a >> b;
c = a + b;
std::cout << c;
}