一、实验要求:
1、构造函数
CMatrix(): 不带参数的构造函数;
CMatrix(int nRow, int nCol, double *pData=NULL) : 带行、列及数据指针等参数的构造函数,并且参数带默认值;
CMatrix(const char * strPath): 带文件路径参数的构造函数;
CMatrix(const CMatrix& m): 拷贝构造函数
此外会用列表初始化成员变量:CMatrix(): m_nRow(0), m_nCol(0), m_pData(NULL);
bool Create(int nRow, int nCol, double *pData=NULL): 先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。
2、析构函数
~CMatrix(): 调用Release();
Release(): 将内存释放,并将行列设置为0;
3、运算符重载
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理
4、友元函数
输入和输出运输符:<<, >>
二、实验过程
1.CMatrix.h
1: #ifndef CMATRIX_H 2: #define CMATRIX_H 3: #include <iostream> 4: using namespace std; 5: class CMatrix 6: { 7: public: 8: CMatrix(); 9: CMatrix(int nRow,int nCol,double *pData=NULL); 10: CMatrix(const CMatrix& m); 11: CMatrix(const char * strPath); 12: ~CMatrix(); 13: bool Create(int nRow,int nCol,double *pData=NULL); 14: void Set(int nRow,int nCol,double dVale); 15: void Release(); 16: friend istream & operator>>(istream& is,CMatrix & m); 17: friend ostream & operator<<(ostream& os,const CMatrix &m); 18: 19: CMatrix& operator=(const CMatrix& m); 20: CMatrix& operator+=(const CMatrix& m); 21: // CMatrix& operator+(const CMatrix& m); 22: // CMatrix operator+(const CMatrix& m1,const CMatrix& m2); 23: double & operator[](int nIndex); 24: double & operator()(int nRow,int nCol); 25: bool operator ==(const CMatrix& m); 26: bool operator !=(const CMatrix& m); 27: 28: operator double(); 29: 30: private: 31: int m_nRow; 32: int m_nCol; 33: double *m_pData; 34: }; 35: CMatrix operator+(const CMatrix& m1,const CMatrix& m2); 36: 37: inline void CMatrix::Set(int nRow,int nCol,double dVal) 38: { 39: m_pData[nRow*m_nCol+nCol]=dVal; 40: } 41: #endif
2.CMatrix.cpp
1: #include "CMatrix.h" 2: #include <fstream> 3: #include <assert.h> 4: CMatrix::CMatrix():m_nRow(0),m_nCol(0),m_pData(0) 5: { 6: 7: } 8: CMatrix::CMatrix(int nRow,int nCol,double *pData):m_pData(0) 9: { 10: Create(nRow,nCol,pData); 11: } 12: CMatrix::CMatrix(const CMatrix& m):m_pData(0) 13: { 14: *this = m; 15: } 16: CMatrix::CMatrix(const char * strPath) 17: { 18: m_pData = 0; 19: m_nRow = m_nCol = 0; 20: ifstream cin(strPath); 21: cin>>*this; 22: } 23: CMatrix::~CMatrix() 24: { 25: Release(); 26: } 27: bool CMatrix::Create(int nRow,int nCol,double *pData) 28: { 29: Release(); 30: m_pData = new double[nRow*nCol]; 31: m_nRow = nRow; 32: m_nCol = nCol; 33: if(pData) 34: { 35: memcpy(m_pData,pData,nRow*nCol*sizeof(double)); 36: } 37: } 38: void CMatrix::Release() 39: { 40: if(m_pData) 41: { 42: delete []m_pData; 43: m_pData = NULL; 44: } 45: m_nRow = m_nCol = 0; 46: } 47: CMatrix& CMatrix::operator=(const CMatrix& m) 48: { 49: if(this!=&m){ 50: Create(m.m_nRow,m.m_nCol,m.m_pData); 51: } 52: return *this; 53: } 54: CMatrix& CMatrix::operator+=(const CMatrix& m) 55: { 56: assert(m_nRow==m.m_nRow && m_nCol==m.m_nCol); 57: for(int i=0;i<m_nRow*m_nCol;i++) 58: { 59: m_pData[i]+=m.m_pData[i]; 60: } 61: return *this; 62: } 63: //CMatrix& CMatrix::operator+(const CMatrix& m) 64: //{ 65: // assert(m_nRow==m.m_nRow && m_nCol==m.m_nCol); 66: // for(int i=0;i<m_nRow*m_nCol;i++) 67: // { 68: // m_pData[i]+=m.m_pData[i]; 69: // } 70: // return *this; 71: //} 72: //CMatrix CMatrix::operator+(const CMatrix& m1,const CMatrix& m2) 73: //{ 74: // CMatrix m3(m1); 75: // m3+=m2; 76: // return m3; 77: //} 78: CMatrix operator+(const CMatrix& m1,const CMatrix& m2) 79: { 80: CMatrix m3(m1); 81: m3 += m2; 82: return m3; 83: } 84: double & CMatrix::operator[](int nIndex) 85: { 86: assert(nIndex<m_nRow*m_nCol); 87: return m_pData[nIndex]; 88: } 89: double & CMatrix::operator()(int nRow,int nCol) 90: { 91: assert(nRow*m_nCol+nCol<m_nRow*m_nCol); 92: return m_pData[nRow*m_nCol+nCol]; 93: } 94: bool CMatrix::operator == (const CMatrix& m) 95: { 96: if(!(m_nRow==m.m_nRow && m_nCol==m.m_nCol)) 97: { 98: return false; 99: } 100: for(int i=0;i<m_nRow*m_nCol;i++) 101: { 102: if(m_pData[i]!=m.m_pData[i]) 103: { 104: return false; 105: } 106: } 107: return true; 108: } 109: bool CMatrix::operator !=(const CMatrix& m) 110: { 111: return !((*this)==m); 112: } 113: CMatrix::operator double() 114: { 115: double dS=0; 116: for(int i=0;i<m_nRow*m_nCol;i++) 117: { 118: dS+=m_pData[i]; 119: } 120: return dS; 121: } 122: 123: istream & operator>>(istream& is,CMatrix & m) 124: { 125: is>>m.m_nRow>>m.m_nCol; 126: m.Create(m.m_nRow,m.m_nCol); 127: for(int i=0;i<m.m_nRow*m.m_nCol;i++) 128: { 129: is>>m.m_pData[i]; 130: } 131: return is; 132: } 133: ostream & operator<<(ostream& os,const CMatrix &m) 134: { 135: os<<m.m_nRow<<" "<<m.m_nCol<<endl; 136: double * pData = m.m_pData; 137: for(int i=0;i<m.m_nRow;i++) 138: { 139: for(int j=0;j<m.m_nCol;j++) 140: { 141: os<<*pData++<<" "; 142: } 143: os<<endl; 144: } 145: return os; 146: }