C++矩阵类的实现

本文档详细介绍了如何在C++中实现一个矩阵类,包括类的定义、成员函数的实现,以及相关操作如矩阵加法、乘法等。通过Matrix.h和Matrix.cpp文件,读者可以理解C++面向对象编程在数值计算中的应用。
摘要由CSDN通过智能技术生成

Matrix.h

#ifndef __MATRIX_H__
#define __MATRIX_H__

class Matrix
{
   
public:
	Matrix(int row, int col);											//普通构造函数
	Matrix(const Matrix& matrix);										//拷贝构造函数
	Matrix();															//构造空矩阵的构造函数
	Matrix& operator = (const Matrix& matrix);							//重载"="操作符
	Matrix& operator += (const Matrix& matrix);							//重载"+="操作符
	int Row(void)const {
    return m_nRow; }								//返回矩阵的行数
	int Col(void)const {
    return m_nCol; }								//返回矩阵的列数
	~Matrix();															//析构函数
	
	//重载"+"操作符
	friend
		Matrix operator + (const Matrix& x, const Matrix& y);
	friend
		Matrix operator + (const Matrix& matrix, double x);
	friend
		Matrix operator + (double x, const Matrix& matrix);

	//重载"-"操作符
	friend
		Matrix operator - (const Matrix& x, const Matrix& y);
	friend
		Matrix operator - (const Matrix& matrix, double x);
	friend
		Matrix operator - (double x, const Matrix& matrix);

	//重载"*"操作符
	friend
		Matrix operator * (const Matrix& x, const Matrix& y);
	friend
		Matrix operator * (const Matrix& matrix, double x);
	friend
		Matrix operator * (double x, const Matrix& matrix);

	//输出矩阵的函数
	friend
		void OutPut(const Matrix& matrix);

	//计算点乘的函数
	friend
		double __DOTMUL(const Matrix& x, const Matrix&y, int nR, int nC);

private:
	int m_nRow;			//矩阵的行数
	int m_nCol;			//矩阵的列数
	double **m_pBuf;	//存储矩阵的空间
};

#endif

Matrix.cpp

//*****头文件*****//
#include "Matrix.h"
#include <iostream>

//*****命名空间*****//
using namespace std;

//*****函数定义*****//
//普通构造函数
inline
Matrix::Matrix(int row, int col)
	:m_nRow(row), m_nCol(col)
{
   
	m_pBuf = new double*[m_nRow];
	for (int i = 0; i < m_nRow; i++)
	{
   
		m_pBuf[i] = new double[m_nCol];
		for (int j = 0; j < m_nCol; j&
好的,下面是一个简单的 C++ 矩阵实现矩阵相乘的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; class Matrix { private: vector<vector<int>> data; // 存储矩阵的二维向量 int m, n; // 矩阵的行数和列数 public: // 构造函数 Matrix(int rows, int cols) { m = rows; n = cols; data.resize(m, vector<int>(n, 0)); // 初始化矩阵为全零矩阵 } // 矩阵相乘运算符重载 Matrix operator*(const Matrix& other) { if (n != other.m) { cout << "矩阵维度不匹配,无法相乘" << endl; return Matrix(0, 0); } Matrix result(m, other.n); for (int i = 0; i < m; i++) { for (int j = 0; j < other.n; j++) { int temp = 0; for (int k = 0; k < n; k++) { temp += data[i][k] * other.data[k][j]; } result.data[i][j] = temp; } } return result; } // 矩阵赋值运算符重载 Matrix& operator=(const Matrix& other) { if (this == &other) { return *this; } m = other.m; n = other.n; data = other.data; return *this; } // 打印矩阵 void print() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << data[i][j] << " "; } cout << endl; } } }; int main() { Matrix A(2, 3); Matrix B(3, 2); A.print(); B.print(); A = {{1, 2, 3}, {4, 5, 6}}; B = {{7, 8}, {9, 10}, {11, 12}}; A.print(); B.print(); Matrix C = A * B; C.print(); return 0; } ``` 这个示例代码中,我们定义了一个 `Matrix` 来表示矩阵,并实现矩阵相乘的运算符重载。在 `main()` 函数中,我们创建了两个矩阵 `A` 和 `B`,并将它们赋值为我们需要相乘的矩阵。然后,我们通过 `A * B` 的方式来计算它们的乘积,并将结果存储在 `C` 中。最后,我们打印了 `C` 的值,即矩阵相乘的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值