编程语言
首页 > 编程语言> > C++设计:关于CMatrix类的相关操作

C++设计:关于CMatrix类的相关操作

作者:互联网

1.头文件声明

#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);
	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

2.函数的实现

              (1)构造器。 本次实验提供四个构造器,下面四个构造器分别为无参构造器,有参构造器,已经复制已有的类创建新的类,通过文件读入构造。

CMatrix::CMatrix() :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;
}
CMatrix::CMatrix(const char* strPath)
{
	m_pData = 0;
	m_nRow = m_nCol = 0;
	ifstream cin(strPath);
	cin >> *this;
}

        (2)下面为对象的创建,删除和析构函数。析构函数为当对象结束其生命周期,系统自动执行析构函数。例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存

bool CMatrix::Create(int nRow, int nCol, double* pData)
{
	Release();
	m_pData = new double[nRow * nCol];
	m_nRow = nRow;
	m_nCol = nCol;
	if (pData != NULL)
	{
		memcpy(m_pData, pData, nRow * nCol * sizeof(double));
		return true;
	}
	else {
		return false;
	}

}
void CMatrix::Release()
{
	if (m_pData!=NULL)
	{
		delete []m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;
}
CMatrix::~CMatrix()
{
	Release();
}

        (3)运算符重载:下面分别对‘=’,‘+=’,‘+’,‘==’,‘!=’进行重载

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;
}
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);
}

        (4)操作符重载:下面分别对‘[]’,‘()’,‘<<’,‘>>'进行重载

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];
}
istream& operator>>(istream& is, CMatrix& m)
{
	is >> m.m_nRow >> m.m_nCol;
	m.Create(m.m_nRow, m.m_nCol,m.m_pData);
	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;
}

        (5)数据类型转换,将创建的对象强制转换成double类

CMatrix::operator double()
{
	double dS = 0;
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		dS += m_pData[i];
	}
	return dS;
}

3.主函数

#include <iostream>
#include <stdio.h>
#include "CMatrix.h"
using namespace std;
int main(int argc, char** argv)
{
	double pData[10] = { 2,3,4,5 };
	CMatrix m1,m2(2, 5, pData), m3("d:\\1.txt"), m4(m2);
	cout << "输入矩阵行,列和对于数量的数值:" << endl;
	cin >> m1;
	m2.Set(1, 3, 10);
	cout << "m1=" << m1 << "\nm2=" << m2 << "\nm3=" << m3 << "\nm4=" << m4;
	m4 = m3;
	m4[2] = m4 + 1;
	if (m4 == m3)
	{
		cout << "Error !" << endl;
	}
	m4 += m3;
	cout << "\nm4=" << m4 << endl;
	cout << "sum of m4 = " << (double)m4 << endl;
	return 0;
}

4.结果展示

 5.总结

        本次实验涉及到类的创建,删除,已经运算符的重载,对类的强制类型转换等等。还有析构函数的使用。本次实验编写明白很多项目构建的问题,其中对于内敛函数,无参函数让系统更快运行也有了深刻影响。对于各类重载方法有了大致的了解。

标签:return,nRow,CMatrix,int,pData,C++,nCol,设计
来源: https://blog.csdn.net/hshagahhva/article/details/120692793