前言
在实验一当中,主要内容为CMatrix类的设计与实现,包含了CMatrix.h,CMatrix.cpp以及main.cpp三大文件。
文章目录
一、实验要求:
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、友元函数
输入和输出运输符:<<, >>
5、作业提交方式
写完请发表博客,上传博客链接
二、实验过程
1.CMatrix.h文件
- 在CMatrix.h头文件当中,要先声明所需要的公有变量以及私有变量。然后根据实验要求,我们需要设计CMatrix构造函数,主要有以下几个函数:
CMatrix(); //不带参数的构造函数
CMatrix(int nRow,int nCol,double *pData=NULL); //带行、列及数据指针等参数的构造函数,并且参数带默认值
CMatrix(const CMatrix& m); //拷贝构造函数,const修饰函数参数使得传递过来的参数在函数内不可改变
CMatrix(const char * strPath);//带文件路径参数的构造函数
这几个函数,虽然它们的函数名相同,但它们具有不同的参数列表,这种方式叫做重载。同时,我们需要再设计一个create函数用于创建矩阵,即bool Create(int nRow, int nCol, double *pData=NULL);另外,再设计一个set内联函数用于矩阵赋值,内联函数可以提高效率; - 然后我们设计析构函数,用于释放内存:
~CMatrix();//析构函数,调用Release方法释放内存
void Release();//将内存释放,并将行列设置为0; - 运算符的重载,包括算数运算符以及关系运算符:
CMatrix& operator+=(const CMatrix& m);
CMatrix operator+(const CMatrix& m1,const CMatrix& m2);
bool operator == (const CMatrix& m);
下标操作符包括:
double & operator[](int nIndex);
double & operator()(int nRow,int nCol);
这里需要注意的是【】代表数组,因此只能有一个参数,而()就可以含有多个参数;
强制类型转换函数:
operator double();
赋值运算符:
CMatrix& operator=(const CMatrix& m);
这里需要注意的是当m1=m1时的特殊情况,若直接调用create函数会先将原有的内存空间进行release释放处理,那么相当于赋值为空,会发生赋值错误,因此需要加个if判断语句避免错误; - 友元函数包括输入和输出函数:
friend istream & operator>>(istream& is,CMatrix & m);//全局函数
friend ostream & operator<<(ostream& os,const CMatrix &m);
若输入和输出函数不设置为友元函数的话,那么程序就会报错说调用了私有变量,友元函数的作用相当于授权给函数能够调用私有变量。
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);//先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中
void set(int nRow,int nCol,double dValue);
// inline void CMatrix::set(int nRow,int nCol,double dVale) //隐含的内联函数
// {
// m_pData[nRow*m_nCol+m_nCol]=dVale;
// }
void Release();//将内存释放,并将行列设置为0;
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);
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 dValue)//显式内联函数
{
m_pData[nRow*m_nCol+m_nCol]=dValue;
}
#endif // CMATRIX_H
2.CMatrix.cpp文件
在CMatrix.cpp文件当中,包括的函数如下:
1.
CMatrix::CMatrix():m_nRow(0),m_nCol(0),m_pData(0) //不带参数的构造函数,初始化成员变量
{
}
这是不带参数的CMatrix构造函数,并初始化了成员变量,同时该函数也可以写成:
CMatrix::CMatrix(