- CMatrix类的实现
- 1、头文件声明:CMatrix.h
- 2、类内部方法,函数的实现:CMatrix.cpp
- 2.1、构造器
- 2.2、析构函数
- 2.3、CMatrix对象方法
- 2.4、运算符重载
- 2.4.1、赋值运算符重载
- 2.4.2、算术运算符重载
- 2.4.3、关系运算符重载
- 2.4.4、下标运算符重载
- 2.4.5、强制类型转换
- 2.5、友元函数
- 3、主函数测试样例:Main.cpp
- 4、运行结果
- 5、总结
CMatrix类的实现 1、头文件声明:CMatrix.h
#ifndef CMATRIX_H #define CMATRIX_H #include2、类内部方法,函数的实现:CMatrix.cpp 2.1、构造器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); //释放内存方法 void Release(); //内联函数 void Set(int nRow, int nCol, double dVale); //友元函数,允许一个函数或类访问类的私有属性 //重载操作符 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); CMatrix& operator-=(const CMatrix& m); bool operator == (const CMatrix& m); bool operator != (const CMatrix& m); double& operator[](int nIndex); double& operator()(int nRow, int nCol); //重载类型转换 operator double(); private: int m_nRow; int m_nCol; double* m_pData = NULL; }; //重载“+”运算符 CMatrix operator+(const CMatrix& m1, const CMatrix& m2); //重载“-”运算符 CMatrix operator-(const CMatrix& m1, const CMatrix& m2); //内联函数(修改矩阵某元素的值) //在编译时,编译器会把内联函数的代码块放置在每个调用该函数的地方 inline void CMatrix::Set(int nRow, int nCol, double dVale) { m_pData[nRow * m_nCol + nCol] = dVale; } #endif
#include "CMatrix.h" #include2.2、析构函数#include //无参构造器 CMatrix::CMatrix() { m_nRow = 0; m_nCol = 0; m_pData = NULL; } //无参构造器(使用初始化表达式) //其中传入参数的顺序和在类间定义时的顺序一致 CMatrix::CMatrix() :m_nRow(0), m_nCol(0), m_pData(0) { //初始化为NULL } //有参构造器 CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0) { Create(nRow, nCol, pData); //调用新建类对象方法 } //拷贝构造函数 //使用一个已经创建完毕的对象来初始化一个新对象 //该新对象是原有对象的浅拷贝 CMatrix::CMatrix(const CMatrix& m) : m_pData(0) { *this = m; //*this表示对象指针,因此只赋值了地址 } //外部数据流构造函数 CMatrix::CMatrix(const char* strPath) { m_pData = 0; m_nRow = m_nCol = 0; ifstream cin(strPath); //通过ifstream定义输入流对象 //将输入流地址赋值给对象指针 cin >> *this; }
//析构函数
//析构函数无参,不可重载
//程序在对象销毁前自动调用析构函数,无需手动调用
CMatrix:: ~CMatrix()
{
Release(); //调用类方法
}
2.3、CMatrix对象方法
对象初始化:
//新建类对象方法
bool CMatrix::Create(int nRow, int nCol, double* pData)
{
//首先在构造前需将其数据指针赋值为空
Release();
//赋值
m_pData = new double[nRow * nCol];
m_nRow = nRow;
m_nCol = nCol;
if (pData)
{
//将传入的pData赋值给类内部变量m_pData(内存拷贝的方法)
memcpy(m_pData, pData, nRow * nCol * sizeof(double));
return true;
}
return false;
}
对象销毁方法:
//销毁对象方法
void CMatrix::Release()
{
//如果指针非空将其指向空
if (m_pData)
{
delete []m_pData;
m_pData = NULL;
}
//初始化0
m_nRow = m_nCol = 0;
}
2.4、运算符重载
2.4.1、赋值运算符重载
//运算符重载(对已有运算符重新定义,赋予其另一种功能,以适应不同的数据类型)
//“=”重载
CMatrix& CMatrix::operator=(const CMatrix& m)
{
//如果自己对自己赋值就直接跳过,这是因为Create方法会首先调用Release(),导致元数据被释放
if (this != &m)
{
//“=”赋值采用Create方法,是深拷贝
Create(m.m_nRow, m.m_nCol, m.m_pData);
}
return *this;
}
2.4.2、算术运算符重载
//“+=”重载
CMatrix& CMatrix::operator+=(const CMatrix& m)
{
//assert断言函数,对括号内的假设进行判断,假如不符合条件就抛出错误,终止程序运行
// 这里的断言函数保证运算符两边的size相等
assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
//内部实现是一个个赋值
m_pData[i] += m.m_pData[i];
}
return *this;
}
//“-=”重载
CMatrix& CMatrix::operator-=(const CMatrix& m)
{
//assert断言函数,对括号内的假设进行判断,假如不符合条件就抛出错误,终止程序运行
//这里的断言函数保证运算符两边的size相等
assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
//内部实现是一个个赋值
m_pData[i] -= m.m_pData[i];
}
return *this;
}
//“+”重载
CMatrix operator+(const CMatrix& m1, const CMatrix& m2)
{
//“+=”重载实现"+"重载
CMatrix m3(m1);
m3 += m2;
return m3;
}
//“-”重载
CMatrix operator-(const CMatrix& m1, const CMatrix& m2)
{
//“-=”重载实现"-"重载
CMatrix m3(m1);
m3 -= m2;
return m3;
}
2.4.3、关系运算符重载
//“==”重载
bool CMatrix::operator == (const CMatrix& m)
{
//如果两者连尺寸都不相等则直接返回不相等
if (!(m_nRow == m.m_nRow && m_nCol == m.m_nCol))
{
return false;
}
//否则一个个比较元素
for (int i = 0; i < m_nRow * m_nCol; i++)
{
if (m_pData[i] != m.m_pData[i])
{
return false;
}
}
return true;
}
//“!=”重载
bool CMatrix::operator !=(const CMatrix& m) {
//“==”重载实现"!="重载
return !((*this) == m);
}
2.4.4、下标运算符重载
//下标操作符[]重载
double& CMatrix::operator[](int nIndex)
{
//保证下标不越界
assert(nIndex < m_nRow* m_nCol);
return m_pData[nIndex];
}
//操作符()重载
double& CMatrix::operator()(int nRow, int nCol)
{
//保证下标不越界
assert(nRow * m_nCol + nCol < m_nRow* m_nCol);
return m_pData[nRow * m_nCol + nCol];
}
2.4.5、强制类型转换
//重载强制类型转换
CMatrix::operator double()
{
double dS = 0;
//将类型转换重载为矩阵所有元素相加
for (int i = 0; i < m_nRow * m_nCol; i++)
{
dS += m_pData[i];
}
return dS;
}
2.5、友元函数
//使得“>>”操作符能够读取 CMatrix 数据类型
istream& operator>>(istream& is, CMatrix& m)
{
is >> m.m_nRow >> m.m_nCol;
//在读取矩阵之前先初始化
m.Create(m.m_nRow, m.m_nCol);
//具体实现是一行行赋值
for (int i = 0; i < m.m_nRow * m.m_nCol; i++)
{
is >> m.m_pData[i];
}
return is;
}
//使得“<<”操作符能够打印 CMatrix 数据类型
ostream& operator<<(ostream& os, const CMatrix& m)
{
os << "size:[" << m.m_nRow << "," << m.m_nCol << ']' << endl;
double* pData = m.m_pData;
//按行列顺序输出矩阵元素
for (int i = 0; i < m.m_nRow; i++)
{
for (int j = 0; j < m.m_nCol; j++)
{
os << *pData++ << " ";
}
os << endl;
}
return os;
}
3、主函数测试样例:Main.cpp
#include4、运行结果 5、总结#include #include "CMatrix.h" using namespace std; int main(int argc, char** argv) { double pData[10] = { 2,3,4,5 }; CMatrix m1, m2(2, 5, pData), m3("1.txt"), m4(m2); cin >> m1; m2.Set(1, 3, 10); cout << m1 << m2 << m3 << m4; m4 = m3; m4[2] = m4 + 1; if (m4 == m3) { cout << "Error !" << endl; } m4 += m3; cout << "sum of m4 = " << (double)m4 << endl; return 0; }
- 在实际应用中,通常需要给每个类定义构造函数。如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现。
- 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作 - 友元函数:
概念: 让一个函数或者类访问另一个类中私有成员,
特殊关键字:friend,
友元的三种实现:
• 全局函数做友元
• 类做友元
• 成员函数做友元
本次实验中用了输入输出符号的重载作为友元函数



