【C++】CMatrix类的构造与创建

一、CMatrix类的代码实现

CMatrix.cpp

#include "CMatrix.h"
#include <fstream>
#include <assert.h>
#include<string.h>
CMatrix::CMatrix() : m_nRow(0), m_nCol(0), m_pData(0) // c++里初始化方式相当于括号内
{
	/*m_nRow = 0;
	m_nCol = 0;
	m_pData = 0;*/
}
CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0)
{
	Create(nRow, nCol, pData);
}
CMatrix::CMatrix(const CMatrix& m) : m_pData(0)
{
	*this = m; // this指向当前类
}
CMatrix::CMatrix(const char* strPath)
{
	m_pData = 0;
	m_nRow = m_nCol = 0;
	ifstream cin(strPath); // 输入文件流
	cin >> *this;
}
CMatrix::~CMatrix()
{
	Release();
}
bool CMatrix::Create(int nRow, int nCol, double* pData)
{
	Release();
	m_pData = new double[nRow * nCol];
	m_nRow = nRow;
	m_nCol = nCol;
	if (pData)
	{
		memcpy(m_pData, pData, nRow * nCol * sizeof(double));
		return true;
	}
	return false;
}
void CMatrix::Release()
{
	if (m_pData)
	{
		delete[]m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;
}
CMatrix& CMatrix::operator=(const CMatrix& m)
{
	if (this != &m) { // 防止将自己赋值给自己时出现内存被释放掉的情况
		Create(m.m_nRow, m.m_nCol, m.m_pData);

	}
	return *this;
}
CMatrix& CMatrix::operator+=(const CMatrix& m)
{
	assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		m_pData[i] += m.m_pData[i];
	}
	return *this;
}
CMatrix operator+(const CMatrix& m1, const CMatrix& m2)
{
	CMatrix m3(m1);
	m3 += m2;
	return m3;
}
double& CMatrix::operator[](int nIndex)
{
	assert(nIndex < m_nRow * m_nCol);
	return m_pData[nIndex];
}
double& CMatrix::operator()(int nRow, int nCol)
{
	assert(nRow * m_nCol + nCol < m_nRow * m_nCol);
	return m_pData[nRow * m_nCol + nCol];
}
bool CMatrix::operator == (const CMatrix& m)
{
	if (!(m_nRow == m.m_nRow && m_nCol == m.m_nCol))
	{
		return false;
	}
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		if (m_pData[i] != m.m_pData[i])
		{
			return false;
		}
	}
	return true;
}
bool CMatrix::operator !=(const CMatrix& m)
{
	return !((*this) == m);
}
CMatrix::operator double()
{
	double dS = 0;
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		dS += m_pData[i];
	}
	return dS;
}
// 对CMatrix的cin,cout
istream& operator>>(istream& is, CMatrix& m)
{
	is >> m.m_nRow >> m.m_nCol;
	m.Create(m.m_nRow, m.m_nCol);
	for (int i = 0; i < m.m_nRow * m.m_nCol; i++)
	{
		is >> m.m_pData[i];
	}
	return is;
}
ostream& operator<<(ostream& os, const CMatrix& m)
{
	os << m.m_nRow << " " << m.m_nCol << endl;
	double* pData = m.m_pData;
	for (int i = 0; i < m.m_nRow; i++)
	{
		for (int j = 0; j < m.m_nCol; j++)
		{
			os << *pData++ << " ";
		}
		os << endl;
	}
	return os;
}

CMatrix.h

#ifndef CMATRIX_H
#define CMATRIX_H
#include <iostream>
using namespace std;
class CMatrix
{
public:
	CMatrix(); //构造函数
	CMatrix(int nRow, int nCol, double* pData = NULL);
	CMatrix(const CMatrix& m);
	CMatrix(const char* strPath);
	~CMatrix(); //析构函数
	bool Create(int nRow, int nCol, double* pData = NULL); //初始化
	void Set(int nRow, int nCol, double dVale);
	void Release(); //释放内存
	// 一序列运算符重载
	friend istream& operator>>(istream& is, CMatrix& m); //声明友元函数,使得非成员函数也可以访问该类的私有成员
	friend ostream& operator<<(ostream& os, const CMatrix& m);

	CMatrix& operator=(const CMatrix& m);
	CMatrix& operator+=(const CMatrix& m);
	//  CMatrix& operator+(const CMatrix& m);
//  CMatrix operator+(const CMatrix& m1,const CMatrix& m2);
	double& operator[](int nIndex);
	double& operator()(int nRow, int nCol);
	bool operator ==(const CMatrix& m);
	bool operator !=(const CMatrix& m);
	//重载强制类型转换
	operator double();

private:
	// 矩阵的行数,列数,数值
	int m_nRow; 
	int m_nCol;
	double* m_pData;
};
CMatrix operator+(const CMatrix& m1, const CMatrix& m2); // 双目运算符重载

inline void CMatrix::Set(int nRow, int nCol, double dVal)
{
	m_pData[nRow * m_nCol + nCol] = dVal;
}
#endif

二、main函数的实现

main.cpp

#include <iostream>
#include "ccomplex.h"
#include<stdio.h>
#include "cmatrix.h"

using namespace std;

int main()
{
    double pData[10] = {2,3,4,5};
    CMatrix m1,m2(2,5,pData),m3("./1.txt"), m4(m2); //测试多个构造器方法是否正确
    cin>>m1; //测试“>>”重载方法
    m2.Set(1,3,10);
    cout<<"----m1-----\n"<<m1<<"----m2-----\n"<<m2<<"----m3-----\n"<<m3<<"----m4-----\n"<<m4;
    m4=m3; //测试“=”重载方法
   m4[1]=m4+1; //测试“[]”重载方法
    if(m4==m3)
    {
        cout<<"Error!!!!!"<<endl;
    }
  	m4 += m3;
 	cout<<"sum of m4 = "<<double(m4)<<endl;//测试相减之后的m4总和
    bool result = (m4>m3);
    cout<<"m4 > m3 the Result is "<<result<<endl;//测试“>”重载结果
    return 0;
}

三、以上代码运行截图

在这里插入图片描述

四、一些重要函数的分析

bool Create(int nRow, int nCol, double *pData=NULL)的作用:先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝m_pData中。
Release(): 的作用:将内存释放,并将行列设置为0;

1.构造函数:

CMatrix();//不带参数的构造函数;
CMatrix(int nRow, int nCol, double *pData=NULL);//带行、列及数据指针等参数的构造函数,并且参数带默认值;
CMatrix(const char * strPath);//带文件路径参数的构造函数;
CMatrix(const CMatrix& m);//拷贝构造函数

构造函数形式为:类名(){}
可以有多个构造函数进行构造(可以重载)
程序在调用对象的时候会自动调用构造

2.析构函数

~CMatrix(): 

析构函数的形式为:~类名(){}
析构函数不可以有参数,因此不可以发生重载
程序在对象销毁前会自动调用析构函数,并且只会调用一次

3.运算符重载

CMatrix & operator=(const CMatrix & m);
CMatrix & operator+=(const CMatrix & m);
double& operator[](int nIndex);
double& operator()(int nRow, int nCol);
bool operator ==(const CMatrix & m);
bool operator !=(const CMatrix & m);
operator double();

运算符重载形式为:operator+“运算符”
运算符重载,可以将类的运算规则自定义
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理

4.友元函数

	friend istream & operator>>(istream& is, CMatrix& m);
	friend ostream & operator<<(ostream & os, const CMatrix & m);

友元函数的形式为:friend+……
在类中声明友元函数使其可以访问类的私有数据
friend和operator同时使用可以让其他类对数据进行的操作符进行自定义
输入和输出运输符:<<, >>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现有两个 CVector 存放数据的自定义动态数组,采用一维动态数组存储矩阵数据 CMatrix 实现的矩阵 使用的时候包含#include "Matrix.h"就行 CMatrix的接口函数都在"Matrix.h"里面 CVector的接口函数在"Vector.h"里,"Matrix.h"里包含了"Vector.h" 具体用法与测试用例Main.cpp里有3个测试用例,分别是针对构造函数属性计算与运算符重载的 内已包含测试工程xp\vc6.0\上亲测通过,并经过BoundsChecker测试没有内存泄漏。有兴趣的童鞋可以下作参考。 注意: 1、下标都是从0开始,数学课上矩阵下标都是从1开始,但是工作后习惯0开始,矩阵M的第一个元素是M(0,0) 2、型定死为double,原来作业是模板,由于vc6对模版支持不好,另矩阵计算double比较理想、整型几乎只能作加减 提供了多种初始化方式,int[]、float[]、double[]均可构造初始化,或则先构造出CVector再由CVector初始化。 3、定义了一个最大允许误差#define permit_eof (1.0e-13),判断相等使用宏 #define EQUAL(a,b) ( ((b) - (a) < permit_eof)&&((a) - (b) < permit_eof) ? (TRUE) : (FALSE) ) 正常输出的时候绝对值小于permit_eof 的时候清零处理,想要指定精度输出请用PrintOut 鸣谢:CSDN上supermegaboy君的C/C++左值性精髓,读后略有所感,空闲时重构了下大学时的作业,着重区分了函数返回值的左右值 =================================================附录:接口函数========================================================================== 开放的接口: CVector //构造函数 CVector(); virtual ~CVector(); CVector(const size_t &nSize;); CVector(const CVector & vIn);//拷贝构造函数 CVector(const double* const pData,const size_t &nSize;); CVector(const float* const pData,const size_t &nSize;); CVector(const int* const pData,const size_t &nSize;); //公开的成员函数 double at(const size_t& uIndex)const;//作右值 BOOL push_back(const double& dbIn ); BOOL resize(const size_t& nSize); size_t size()const; //重载操作符 double& operator()(const UINT& uIndex);//重载()运算符,可作左值 //重载的运算符 double& operator()(const size_t& xr,const size_t& xc);//重载()运算符,可作左值 CVector& operator=(const CVector &);//重载=运算符 double operator*(const CVector & )const;//重载*运算符,两向量相乘 CVector operator*(const double α)const;//重载*运算符,向量乘以实数alpha CVector& operator*=(const double α);//重载*=算符,向量乘以实数alpha CVector operator+(const CVector & )const;//重载+运算符,向量加上向量 CVector& operator+=(const CVector & );//重载+=算符,向量加上向量 CVector operator-(const CVector & )const;//重载+运算符,向量加上向量 CVector& operator-=(const CVector & );//重载+=算符,向量加上向量 CVector operator+(const double α)const;//重载+运算符,向量加上实数alpna CVector& operator+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打代码能当饭吃?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值