栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ —— 实现CMatrix类

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ —— 实现CMatrix类

一、CMatrix代码实现 1、main.cpp
#include
#include"CMatrix.h"
using namespace std;
int main() {
    double pData[10] = { 12,23,34,45 };
    CMatrix m1, m2(2, 5, pData);
    cin >> m1;
    m2.Set(1, 3, 18);
    cout << m1 << m2;
    CMatrix ms[4] = { CMatrix(),CMatrix(2,5,pData),CMatrix(ms[1]),CMatrix("D:\1.txt") };
    cout << ms[1] << ms[2];
    if (ms[1] != ms[2])
    {
        cout << "Error occur!" << endl;
    }
    ms[1] += ms[2];
    ms[1][1] = 100;
    ms[1](1, 1) = 50;
    cout << ms[1];
    cout << "sum of m1=" << double(ms[1]);
    double d = 1.2;
    int i = int(d);
	return 0;
}
2、CMatrix.h
#pragma once
#include
using namespace std;

class CMatrix {
public:
	CMatrix();//不带参数的构造函数;
	CMatrix(int nRow, int nCol, double* pData = NULL);//带行、列及数据指针等参数的构造函数,并且参数带默认值;
	CMatrix(const CMatrix& m);//拷贝构造函数
	CMatrix(const char* strPath);//带文件路径参数的构造函数;
	~CMatrix();//析构函数
	//先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。
	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;
}
3、CMatrix.cpp
#include "CMatrix.h"
#include
#include
#include
using namespace std;
CMatrix::CMatrix() {
	m_nRow = 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_nCol = m_nRow = 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_nCol = nCol;
	m_nRow = nRow;
	if (pData) {
		memcpy(m_pData, pData, nRow * nCol * sizeof(double));
	}
	return true;
}
void CMatrix::Release() {
	if (m_pData) {
		delete[] m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;
}

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_nCol * m.m_nRow; i++) {
		is >> m.m_pData[i];
	}
	return is;
}
ostream& operator<<(ostream& os, const CMatrix& m)
{
	os << "m_nRow  " << m.m_nRow << " " << "m_nCol  " << 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& 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 + 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;
}
二、运行结果

输入任意矩阵规模(3*3)以及矩阵

 三、总结 1、构造函数

  • 构造函数,没有返回值也不写void
  • 函数名称和类名相同
  • 构造函数可以有参数,因此可以发生重载
  • 程序在调用对象时候会自动调用构造,无须手动调用,并且只会调用一次
  •  double* pData=NULL 无pData传入时默认进行赋值null 
2、析构函数

  • 析构函数,没有返回值也不写void
  • 函数名称与类名相同,在名称前加上符号~
  • 析构函数不可以有参数,因此不可以发生重载
  • 程序在对象销毁前会自动调用析构,无须手动调用,并且只会调用一次 
3、运算符重载

  • CMatrix & CMatrix:: operator = (const CMatrix &m)表示一个运算符重载函数,在理解时可将operator和运算符(如operator=)视为一个函数名。
  • this是指向自身对象的指针,*this是自身对象。也就是说return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 )。return this返回当前对象的地址(指向当前对象的指针)。
  • CMatrix:: operator double()函数无需指明返回类型,double作为一个运算符,该函数用于double运算符重载。
 4、友元函数

  • 在类中声明友元函数使其可以访问类的私有数据
  • friend和operator同时使用可以让其他类对数据进行的操作符进行自定义

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号