文章目录
实验背景
本次实验基于vscode下的c++环境,实现了关于CMatrix系列函数和运算符的重载
1.函数解析
#ifndef CMATRIX_H
#define CMATRIX_H
#include<iostream>
#include<string.h>
using namespace std;
//二维数组
class CMatrix
{
//类属性
private:
//行数
int m_nRow;
//列数
int m_nCol;
//数据
double *m_pData=NULL;
public:
//默认构造函数
CMatrix();
//带行,列,数据指针参数的构造函数
CMatrix(int nRow,int nCol,double *pData=NULL);
//拷贝构造函数
CMatrix(const CMatrix &cc);
//带文件路径的构造函数
CMatrix(const char*strPath);
//释放内存的析构函数,调用Release();
~CMatrix();
void Release();
//创建空间,令分配空间和实际空间匹配
bool Create(int nRow,int nCol,double *pData=NULL);
//释放内存
//修改数据值;内联函数,提高效率(代码尽量简洁);循环,递归最好不要写成内联
void Set(int nRow,int nCol,double dVall)
{
m_pData[nRow*m_nCol+nCol]=dVall;
}
//友元函数,授权声明,赋予访问权限
friend istream& operator>>(istream& is,CMatrix&cc);
friend ostream& operator<<(ostream& os,const CMatrix&cc);
//运算符重载:+,+=,-,-=;
CMatrix& operator +=(const CMatrix&cc);
CMatrix& operator -=(const CMatrix&cc);
//下标操作符:[],()
double & operator[](int nIndex);
double & operator()(int nRow,int nCol);
//强制类型转换:double
operator double();
//赋值运算符:=
CMatrix& operator =(const CMatrix&cc);
//关系运算符重载,>,<,==,!=
bool operator ==(const CMatrix&cc);
bool operator !=(const CMatrix&cc);
bool operator >(const CMatrix&cc);
bool operator <(const CMatrix&cc);
};
CMatrix operator -(const CMatrix&