There are two rows of the matrix was 3 ml and m2 columns 3 so that it can be used for matrices m1 and m2 are added, the input and output.

There are two rows of the matrix was 3 ml and m2 columns 3, to achieve programming matrices Matrix, carrying both operator "+", ">>" and "<<", so that it can be used matrices m1 and m2 addition, the input and output.

//Matrix .h
#include <iostream>
using namespace std;
class Matrix
{
public:
	Matrix();
	~Matrix();
	Matrix(const Matrix&);
	friend Matrix  operator+(const Matrix&a,const Matrix&b);
	friend ostream & operator<<(ostream&a,const Matrix&b);
	friend istream & operator>>(istream&a, Matrix&b);
private:
	double a[3][3];
};

//Matrix cpp
#include "pch.h"
#include "Matrix.h"
Matrix::Matrix()
{
}
Matrix::~Matrix()
{
}
Matrix::Matrix(const Matrix&b)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			a[i][j] = b.a[i][j];
		}
	}
}
Matrix  operator+( const Matrix&a,const  Matrix&b)
{
	Matrix p;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			p.a[i][j]=a.a[i][j] + b.a[i][j];
		}
	}
	return p;
}
ostream& operator<<(ostream&a,const Matrix&b)
{

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			a << b.a[i][j] << '\t';
		}
		cout << endl;
	}
	return a;
}
istream & operator>>(istream &a, Matrix &b)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			a >>b.a[i][j];
		}
	}
	return a;
}

//主函数
#include "pch.h"
#include "Matrix.h"
#include <iostream>
using namespace std;
int main()
{
	Matrix a, b;
	cin >> a >> b;
	cout << a + b;
}

Guess you like

Origin blog.csdn.net/weixin_43981315/article/details/94759706