用模板技术,实现一个矩阵类CMatrixT,基本要求如下:1.实现构造、拷贝构造与析构函数;2.支持任意大小的矩阵,行数和列数可以动态设置(不用修改代码);3.重载操作符*,实现矩阵相乘;4.重载操作符=,实现矩阵赋值;5.重载操作符++和--,分别实现矩阵元素同时+1和-1;在main函数中对以上功能进行调用。用模板技术
时间: 2024-03-07 12:46:52 浏览: 64
矩阵类实现matrix
好的,下面是实现矩阵类CMatrixT的代码:
```c++
#include <iostream>
using namespace std;
template <typename T>
class CMatrixT
{
public:
CMatrixT(int row, int col); // 构造函数
CMatrixT(const CMatrixT& other); // 拷贝构造函数
~CMatrixT(); // 析构函数
void Resize(int row, int col); // 重设矩阵大小
CMatrixT operator*(const CMatrixT& other); // 重载*操作符
CMatrixT& operator=(const CMatrixT& other); // 重载=操作符
CMatrixT& operator++(); // 重载++操作符
CMatrixT& operator--(); // 重载--操作符
void Print(); // 打印矩阵
private:
int m_row; // 矩阵行数
int m_col; // 矩阵列数
T** m_data; // 矩阵数据指针
};
template <typename T>
CMatrixT<T>::CMatrixT(int row, int col)
{
m_row = row;
m_col = col;
m_data = new T*[m_row];
for (int i = 0; i < m_row; ++i)
{
m_data[i] = new T[m_col];
for (int j = 0; j < m_col; ++j)
{
m_data[i][j] = 0;
}
}
}
template <typename T>
CMatrixT<T>::CMatrixT(const CMatrixT& other)
{
m_row = other.m_row;
m_col = other.m_col;
m_data = new T*[m_row];
for (int i = 0; i < m_row; ++i)
{
m_data[i] = new T[m_col];
for (int j = 0; j < m_col; ++j)
{
m_data[i][j] = other.m_data[i][j];
}
}
}
template <typename T>
CMatrixT<T>::~CMatrixT()
{
for (int i = 0; i < m_row; ++i)
{
delete[] m_data[i];
}
delete[] m_data;
}
template <typename T>
void CMatrixT<T>::Resize(int row, int col)
{
T** new_data = new T*[row];
for (int i = 0; i < row; ++i)
{
new_data[i] = new T[col];
for (int j = 0; j < col; ++j)
{
new_data[i][j] = 0;
}
}
for (int i = 0; i < min(m_row, row); ++i)
{
for (int j = 0; j < min(m_col, col); ++j)
{
new_data[i][j] = m_data[i][j];
}
}
for (int i = 0; i < m_row; ++i)
{
delete[] m_data[i];
}
delete[] m_data;
m_row = row;
m_col = col;
m_data = new_data;
}
template <typename T>
CMatrixT<T> CMatrixT<T>::operator*(const CMatrixT& other)
{
if (m_col != other.m_row)
{
throw "Invalid matrix size!";
}
CMatrixT<T> res(m_row, other.m_col);
for (int i = 0; i < m_row; ++i)
{
for (int j = 0; j < other.m_col; ++j)
{
for (int k = 0; k < m_col; ++k)
{
res.m_data[i][j] += m_data[i][k] * other.m_data[k][j];
}
}
}
return res;
}
template <typename T>
CMatrixT<T>& CMatrixT<T>::operator=(const CMatrixT& other)
{
if (this == &other)
{
return *this;
}
Resize(other.m_row, other.m_col);
for (int i = 0; i < m_row; ++i)
{
for (int j = 0; j < m_col; ++j)
{
m_data[i][j] = other.m_data[i][j];
}
}
return *this;
}
template <typename T>
CMatrixT<T>& CMatrixT<T>::operator++()
{
for (int i = 0; i < m_row; ++i)
{
for (int j = 0; j < m_col; ++j)
{
++m_data[i][j];
}
}
return *this;
}
template <typename T>
CMatrixT<T>& CMatrixT<T>::operator--()
{
for (int i = 0; i < m_row; ++i)
{
for (int j = 0; j < m_col; ++j)
{
--m_data[i][j];
}
}
return *this;
}
template <typename T>
void CMatrixT<T>::Print()
{
for (int i = 0; i < m_row; ++i)
{
for (int j = 0; j < m_col; ++j)
{
cout << m_data[i][j] << " ";
}
cout << endl;
}
}
```
下面是使用该类的main函数:
```c++
int main()
{
CMatrixT<int> mat1(2, 3);
mat1.Print();
CMatrixT<int> mat2(3, 2);
mat2.Print();
CMatrixT<int> mat3 = mat1 * mat2;
mat3.Print();
CMatrixT<int> mat4(2, 2);
mat4 = mat3;
mat4.Print();
++mat4;
mat4.Print();
--mat4;
mat4.Print();
return 0;
}
```
完整代码已经在本文的附件中提供。
阅读全文