C++实现CMatrix类的构造、实现及运算符重载
前言
CMatrix类及方法均为对矩阵的操作,operator+“运算符”的方法可进行运算符的重载,实现替代编译器提供的默认的赋值运算符重载函数
一、CMatrix类的实现代码
CMatrix.cpp
#include "CMatrix.h"
#include <fstream>
#include <assert.h>
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;
}
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));
}
}
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& 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 CMatrix::operator+(const CMatrix& m1,const CMatrix& m2)
//{
// CMatrix m3(m1);
// m3+=m2;
// return m3;
//}
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;
}
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.cpp
#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:\\QT\\codes\\code01\\1.txt"),m4(m2);
cin>>m1;
m2.Set(1,3,10);
cout<<"m1=========\n"<<m1<<"m2=========\n"<<m2<<"m3=========\n"<<m3<<"m4=========\n"<<m4<<endl;
m4=m3;
m4[2]=m4+1;
if(m4==m3)
{
cout<<"Error !"<<endl;
}
m4 += m3;
cout<<m4<<endl;
cout<<"sum of m4 = "<<(double)m4<<endl;
return 0;
}
二、运行截图

三、总结
构造函数
CMatrix();
CMatrix(int nRow, int nCol, double* pData = NULL);
CMatrix(const CMatrix & m);
CMatrix(const char* strPath);
构造函数形式为:类名(){}
1.没有返回值也不写void
2.可以有参数,所以可以发生重载
3.程序在调用对象的时候会自动调用构造
析构函数
~CMatrix();
析构函数的形式为:~类名(){}
1.没有返回值也不写void
2.不可以有参数,所以不可以发生重载
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+“运算符”
1.之所以称为重载,是因为编译器本身有提供默认的运算符重载函数
2.运算符的重载可以自定义运算符的运算规则
3.对象要以指针的形式是为了返回地址确保运算确实成功了,而不是改变临时变量
友元函数
友元函数的形式为:friend+……
friend istream & operator>>(istream& is, CMatrix& m);
friend ostream & operator<<(ostream & os, const CMatrix & m);
1.友元函数定义在类的外部,但有权访问类的变量
2.流运算符<<,>>不能定义为类的成员函数,只能是友元函数
内联函数
inline void CMatrix::Set(int nRow, int nCol, double dVal)
{
m_pData[nRow * m_nCol + nCol] = dVal;
}
内联函数的形式为:inline+……
1.关键字inline必须与函数定义体放在一起
2.内联是以代码复制为代价,省去了函数调用的开销,从而提高代码运行效率
这篇博客介绍了如何使用C++实现CMatrix类,包括构造函数、析构函数以及运算符重载。文章详细展示了CMatrix类的源代码,如赋值运算符、加法运算符的重载,并提供了友元函数和内联函数的实现,以支持从文件读取和输出矩阵。此外,还展示了如何通过main.cpp文件测试这些功能的运行效果。
4921

被折叠的 条评论
为什么被折叠?



