CMatrix类设计

本文详细介绍了CMatrix类的设计,包括构造函数、析构函数、运算符重载、友元函数和内联函数的实现。通过CMatrix类,深入理解C++的类与对象、多态以及内存管理。同时,给出了代码实现和运行结果。
摘要由CSDN通过智能技术生成


前言

通过对CMatrix类的设计熟悉c++的类与对象,多态,构造函数,学习如何使用c++。

 

一、实验内容

一、构造函数
        1.CMatrix(): 不带参数的构造函数;
        2.CMatrix(int nRow, int nCol, double *pData=NULL) : 带行、列及数据指针等参数的构造函数, 并且参数带默认值;
        3.CMatrix(const char * strPath): 带文件路径参数的构造函数;
        4.CMatrix(const CMatrix& m): 拷贝构造函数
        5.此外会用列表初始化成员变量:CMatrix(): m_nRow(0), m_nCol(0), m_pData(NULL);
        6.bool Create(int nRow, int nCol, double *pData=NULL): 先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。
        二、析构函数
        1.~CMatrix(): 调用Release();
        2.Release(): 将内存释放,并将行列设置为0;
        三、运算符重载
        1.算术运算符重载:+, -, +=, -=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMatrix需要重载的运算符有:加法运算符、减法运算符、乘法运算符、赋值运算符、相等运算符、取反运算符、输入运算符和输出运算符。 下面是补充完整的CMatrix代码: ```cpp #include<iostream> using namespace std; class CMatrix{ public: CMatrix(int r=0,int c=0):row(r),col(c){ //构造函数 if(row*col==0) ptr=NULL; else{ ptr=new double*[row]; for(int i=0;i<row;i++) ptr[i]=new double[col]; } } CMatrix(const CMatrix& m):row(m.row),col(m.col){ //拷贝构造函数 if(row*col==0) ptr=NULL; else{ ptr=new double*[row]; for(int i=0;i<row;i++){ ptr[i]=new double[col]; for(int j=0;j<col;j++) ptr[i][j]=m.ptr[i][j]; } } } ~CMatrix(){ //析构函数 if(ptr!=NULL){ for(int i=0;i<row;i++) delete[]ptr[i]; delete[]ptr; } } CMatrix operator+(const CMatrix& m)const; //加法运算符重载 CMatrix operator-(const CMatrix& m)const; //减法运算符重载 CMatrix operator*(const CMatrix& m)const; //乘法运算符重载 CMatrix& operator=(const CMatrix& m); //赋值运算符重载 bool operator==(const CMatrix& m)const; //相等运算符重载 CMatrix operator-()const; //取反运算符重载 friend istream& operator>>(istream& is,CMatrix& m); //输入运算符重载 friend ostream& operator<<(ostream& os,const CMatrix& m); //输出运算符重载 private: int row,col; double **ptr; }; CMatrix CMatrix::operator+(const CMatrix& m)const{ //加法运算符重载 if(row!=m.row || col!=m.col) throw "加法运算符重载:矩阵维度不一致!"; CMatrix temp(row,col); for(int i=0;i<row;i++) for(int j=0;j<col;j++) temp.ptr[i][j]=ptr[i][j]+m.ptr[i][j]; return temp; } CMatrix CMatrix::operator-(const CMatrix& m)const{ //减法运算符重载 if(row!=m.row || col!=m.col) throw "减法运算符重载:矩阵维度不一致!"; CMatrix temp(row,col); for(int i=0;i<row;i++) for(int j=0;j<col;j++) temp.ptr[i][j]=ptr[i][j]-m.ptr[i][j]; return temp; } CMatrix CMatrix::operator*(const CMatrix& m)const{ //乘法运算符重载 if(col!=m.row) throw "乘法运算符重载:矩阵维度不匹配!"; CMatrix temp(row,m.col); for(int i=0;i<row;i++) for(int j=0;j<m.col;j++){ temp.ptr[i][j]=0; for(int k=0;k<col;k++) temp.ptr[i][j]+=ptr[i][k]*m.ptr[k][j]; } return temp; } CMatrix& CMatrix::operator=(const CMatrix& m){ //赋值运算符重载 if(ptr==m.ptr) return *this; if(row*col!=m.row*m.col){ for(int i=0;i<row;i++) delete[]ptr[i]; delete[]ptr; row=m.row; col=m.col; ptr=new double*[row]; for(int i=0;i<row;i++) ptr[i]=new double[col]; } for(int i=0;i<row;i++) for(int j=0;j<col;j++) ptr[i][j]=m.ptr[i][j]; return *this; } bool CMatrix::operator==(const CMatrix& m)const{ //相等运算符重载 if(row!=m.row || col!=m.col) return false; for(int i=0;i<row;i++) for(int j=0;j<col;j++) if(ptr[i][j]!=m.ptr[i][j]) return false; return true; } CMatrix CMatrix::operator-()const{ //取反运算符重载 CMatrix temp(row,col); for(int i=0;i<row;i++) for(int j=0;j<col;j++) temp.ptr[i][j]=-ptr[i][j]; return temp; } istream& operator>>(istream& is,CMatrix& m){ //输入运算符重载 for(int i=0;i<m.row;i++) for(int j=0;j<m.col;j++) is>>m.ptr[i][j]; return is; } ostream& operator<<(ostream& os,const CMatrix& m){ //输出运算符重载 for(int i=0;i<m.row;i++){ for(int j=0;j<m.col;j++) os<<m.ptr[i][j]<<" "; os<<endl; } return os; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值