C++矩阵类的实现

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&
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值