一、头文件的创建
C++对于创建一个类以及各项方法的推荐做法是在头文件.h中将类中所有的函数,变量预先定义,之后再在另一个.cpp文件中将这些所有方法一一实现,所以我们需要将我们对于矩阵类(我们之后都用CMatrix来表示这个类)的所有变量都在CMatrix.h中全部定义出来
#ifndef CMATRIX_H
#define CMATRIX_H
#include <iostream>
#include <string.h>
using namespace std;
class CMatrix
{
public:
CMatrix();
CMatrix(int nRow,int nCol,double *pData=NULL);
CMatrix(const char * strPath);
CMatrix(const CMatrix & m);
~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);
double & operator[](int nIndex);
double & operator()(int nRow,int nCol)
{
return m_pData[nRow*m_nCol+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
。
二、各类构造器的创建
接下来我们都是在CMatrix.cpp文件下实现这些方法,先引用一下头文件
#include "cmatrix.h"
#inclu