第七次的模板类编写要求挺高的,鉴于大家的作业完成得不理想,决定本次作业不纳入最后的作业考核中,只供大家练习(大家没意见吧?)。
对于模板类的几点解释:
一:模板类比较特殊,目前绝大多数编译器不支持模板的分离编译模式,所以有3个方案,
(1)将模板类的声明和定义放到一个头文件(.H文件)中;
(2)或者声明和定义还是分开来写,可以在.h文件的最后将.cpp文件包含进来(参考代码选用这种方式);
(3)将类和主函数写在一个CPP文件里。(个人比较推荐这种方式应付考试)
(之前几位同学问我的时候我没有去试过,说的不对,误导了,先道个歉,以这次反馈为准)
PS:不多说了,大家直接看参考答案吧。。
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
template <typename elemType, int row = 4, int col = 4>
class Matrix
{
friend class Matrix;
template <typename elemType, int row, int col>
friend ostream& operator << (ostream&, const Matrix<elemType, row, col>&);
public:
Matrix();
Matrix(const Matrix&);
~Matrix();
void ReadData();
elemType Max() const;
elemType Min() const;
template <int irow, int icol>
bool operator == (const Matrix<elemType, irow, icol>&) const;
Matrix operator + (const Matrix&) const;
template<int icol>
Matrix<elemType, row, icol> operator * (const Matrix<elemType, col, icol>&) const;
private:
int _row, _col;
elemType **_matrix;
};
#include "Matrix.cpp"
#endif
Matrix.cpp
#ifndef MATRIX_CPP
#define MATRIX_CPP
#include "Matrix.h"
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename elemType, int row, int col>
Matrix<elemType, row, col>::Matrix()
: _row(row), _col(col)
{
_matrix = new elemType*[_row];
for (int i = 0; i < _row; i ++)
{
_matrix[i] = new elemType[_col];
for (int j = 0; j < _col; j ++)
_matrix[i][j] = elemType();
}
}
template <typename elemType, int row, int col>
Matrix<elemType, row, col>::Matrix(const Matrix& rhs)
: _row(rhs._row), _col(rhs._col)
{
_matrix = new elemType*[_row];
for (int i = 0; i < _row; i ++)
{
_matrix[i] = new elemType[_col];
for (int j = 0; j < _col; j ++)
_matrix[i][j] = rhs._matrix[i][j];
}
}
template <typename elemType, int row, int col>
Matrix<elemType, row, col>::~Matrix()
{
for (int i = 0; i < _row; i ++)
delete[] _matrix[i];
delete[] _matrix;
}
template <typename elemType, int row, int col>
void Matrix<elemType, row, col>::ReadData()
{
cout << "Please Input An Matrix Of " << _row << " * " << _col << endl;
for (int i = 0; i < _row; i ++)
for (int j = 0; j < _col; j ++)
cin >> _matrix[i][j];
}
template <typename elemType, int row, int col>
elemType Matrix<elemType, row, col>::Max() const
{
elemType max(_matrix[0][0]);
for (int i = 0; i < _row; i ++)
for (int j = 0; j < _col; j ++)
if (_matrix[i][j] > max)
max = _matrix[i][j];
return max;
}
template <typename elemType, int row, int col>
elemType Matrix<elemType, row, col>::Min() const
{
elemType min(_matrix[0][0]);
for (int i = 0; i < _row; i ++)
for (int j = 0; j < _col; j ++)
if (min > _matrix[i][j])
min = _matrix[i][j];
return min;
}
template <typename elemType, int row, int col>
template <int irow, int icol>
bool Matrix<elemType, row, col>::operator == (const Matrix<elemType, irow, icol>& rhs) const
{
if (_row != irow || _col != icol)
return false;
for (int i = 0; i < _row; i ++)
for (int j = 0; j < _col; j ++)
if (_matrix[i][j] != rhs._matrix[i][j])
return false;
return true;
}
template <typename elemType, int row, int col>
Matrix<elemType, row, col> Matrix<elemType, row, col>::operator + (const Matrix& rhs) const
{
Matrix<elemType, row, col> tmp;
for (int i = 0; i < _row; i ++)
for (int j = 0; j < _col; j ++)
tmp._matrix[i][j] = _matrix[i][j] + rhs._matrix[i][j];
return tmp;
}
template <typename elemType, int row, int col>
template <int icol>
Matrix<elemType, row, icol> Matrix<elemType, row, col>::operator * (const Matrix<elemType, col, icol>& rhs) const
{
Matrix<elemType, row, icol> tmp;
for (int i = 0; i < _row; i ++)
for (int j = 0; j < rhs._col; j ++)
{
bool plus = false;
for (int k = 0; k < _col; k ++)
if (!plus)
{
tmp._matrix[i][j] = _matrix[i][k] * rhs._matrix[k][j];
plus = true;
}
else
tmp._matrix[i][j] = tmp._matrix[i][j] + _matrix[i][k] * rhs._matrix[k][j];
}
return tmp;
}
template <typename elemType, int row, int col>
inline ostream& operator << (ostream& os, const Matrix<elemType, row, col>& rhs)
{
for (int i = 0; i < rhs._row; i ++)
{
for (int j = 0; j < rhs._col; j ++)
os << rhs._matrix[i][j] << " ";
os << endl;
}
return os;
}
#endif
main.cpp
#include "Matrix.h"
#include <iostream>
using namespace std;
int main()
{
Matrix<int, 2, 3> matrix_1;
matrix_1.ReadData();
Matrix<int, 2, 3> matrix_2(matrix_1);
Matrix<int, 3, 2> matrix_3;
matrix_3.ReadData();
cout << "matrix_1:\n" << matrix_1;
cout << "matrix_1's maximum number: " << matrix_1.Max() << endl
<< "matrix_1's minimum number: " << matrix_1.Min() << endl;
cout << "matrix_1 " << (matrix_1 == matrix_2 ? "" : "not") << "equal to matrix_2" << endl;
cout << "matrix_1:\n" << matrix_1;
cout << "matrix_2:\n" << matrix_2;
cout << "matrix_1 plus matrix_2" << endl;
cout << matrix_1 + matrix_2 << endl;
cout << "matrix_1 " << (matrix_1 == matrix_3 ? "" : "not") << "equal to matrix_3" << endl;
cout << "matrix_1:\n" << matrix_1;
cout << "matrix_3:\n" << matrix_3;
cout << "matrix_1 multi matrix_3" << endl
<< matrix_1 * matrix_3;
return 0;
}