CMatrix类的实现
1、头文件声明: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);
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
2、类内部方法,函数的实现:CMatrix.cpp
2.1、构造器
#include "CMatrix.h"
#include <fstream>
#include <assert.h>
CMatrix::CMatrix()
{
m_nRow = 0;
m_nCol = 0;
m_pData = NULL;
}
CMatrix::CMatrix() :m_nRow(0), m_nCol(0), m_pData(0)
{
}
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;
}
CMatrix::CMatrix(const char* strPath) {
m_pData = 0;
m_nRow = m_nCol = 0;
ifstream cin(strPath);
cin >> *this;
}
2.2、析构函数
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)
{
memcpy(m_pData, pData, nRow * nCol * sizeof(double));
return true;
}
return false;
}
对象销毁方法:
void CMatrix::Release()
{
if (m_pData)
{
delete []m_pData;
m_pData = NULL;
}
m_nRow = m_nCol = 0;
}
2.4、运算符重载
2.4.1、赋值运算符重载
CMatrix& CMatrix::operator=(const CMatrix& m)
{
if (this != &m)
{
Create(m.m_nRow, m.m_nCol, m.m_pData);
}
return *this;
}
2.4.2、算术运算符重载
CMatrix& CMatrix::operator+=(const CMatrix& m)
{
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(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、友元函数
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;
}
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
#include <iostream>
#include <stdio.h>
#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;
}
4、运行结果
5、总结
- 在实际应用中,通常需要给每个类定义构造函数。如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现。
- 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作 - 友元函数:
概念: 让一个函数或者类访问另一个类中私有成员, 特殊关键字:friend, 友元的三种实现: ? 全局函数做友元 ? 类做友元 ? 成员函数做友元 本次实验中用了输入输出符号的重载作为友元函数
|