53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
#include <iostream>
|
|
using namespace std;
|
|
class matrix { // 该类的定义
|
|
public:
|
|
matrix(double a1 = 1, double a2 = 2, double a3 = 3, double a4 = 4) {
|
|
a[0][0] = a1;
|
|
a[0][1] = a2;
|
|
a[1][0] = a3;
|
|
a[1][1] = a4;
|
|
}
|
|
matrix operator+(matrix &);
|
|
friend ostream &operator<<(ostream &, matrix &); // 输出流运算符重载
|
|
|
|
private:
|
|
double a[2][2];
|
|
};
|
|
|
|
matrix matrix::operator+(matrix &b) { // 加法的定义
|
|
int i, j;
|
|
matrix c(0, 0, 0, 0);
|
|
for (i = 0; i < 2; i++) {
|
|
for (j = 0; j < 2; j++) {
|
|
c.a[i][j] = a[i][j] + b.a[i][j];
|
|
}
|
|
}
|
|
return c;
|
|
}
|
|
|
|
ostream &operator<<(ostream &out, matrix &b) { // “<<”的定义
|
|
int i, j;
|
|
for (i = 0; i < 2; i++) {
|
|
for (j = 0; j < 2; j++) {
|
|
out << b.a[i][j] << " ";
|
|
}
|
|
out << endl;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
int main() {
|
|
matrix a(1.1, 1.2, 1.3, 1.4), b(2.1, 2.2, 2.3, 2.4), c;
|
|
c = a + b;
|
|
cout << "矩阵a + b等于" << endl;
|
|
cout << c;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
矩阵a + b等于
|
|
3.2 3.4
|
|
3.6 3.8
|
|
*/
|