1.构造函数:类通过一个或者几个特殊的成员函数来控制其对象的初始化过程。
1.1CMatrix():不带参数的构造函数
源代码:
CMatrix::CMatrix():m_nRow(0),m_nCol(0),m_pData(0)
{
}
1.2带行,列以及数据指针等参数的构造函数源代码:
CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0)
{
Create(nRow, nCol, pData);
}
1.3带文件路径参数的构造函数
源代码:
CMatrix::CMatrix(const char* strPath)
{
m_pData = 0;
m_nRow = m_nCol = 0;
ifstream cin(strPath);
cin >> *this;
}
1.4复制构造函数:也称拷贝函数,它只有一个参数,参数类型是对类的引用。参数可以是const引用,也可以是非const引用。
源代码:
CMatrix::CMatrix(const CMatrix & m) : m_pData(0)
{
*this = m;
}
2析构函数:它的名字与类名相同,但是前面要加~,没有参数和返回值
一个类有且只有一个析构函数。定义类时如果没有定义析构函数,则编译器会自动生成默认析构函数。
对象消亡时析构函数会被自动调用。
2.1~CMatrix(): 调用Release();
CMatrix::~CMatrix()
{
Release();
}
3 CMatrix对象方法
3.1对象初始化:bool CMatrix::Create(int nRow, int nCol, double* pData)
源代码:
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)); //memcpy内存拷贝函数,从pData中拷贝nRow * nCol * sizeof(double)个字节的内容到m_pData中
}
}
3.2对象销毁:将内存释放,将行列设置为0
源代码:
void CMatrix::Release()
{
if (m_pData)
{
delete[]m_pData; //内存释放
m_pData = NULL;
}
m_nRow = m_nCol = 0; //将行列设置为0
}
4 运算符重载
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理
输入和输出运输符:<<, >>
5 友元函数:可以直接访问类的私有成员的非成员函数,它是定义在类外的普通函数,它不属于任何类,但是需要在类的定义中加以声明,声明是需要在友元函数的名称前加friend.
6主函数以及头文件
6.1 主函数main.cpp
#include<iostream>
using namespace std;
#include"CMatrix.h"
int main()
{
double pData[10] = { 2,3,4,5 };
CMatrix m1, m2(2, 5, pData);
cin >> m1;
m2.Set(1, 3, 10);
cout << m1 << m2;
CMatrix ms[4] = { CMatrix(),CMatrix(2,5,pData),CMatrix(ms[1]),CMatrix("C:\\1.txt") };
cout << ms[1] << ms[2];
if (ms[1] != ms[2])
{
cout << "Error occur!" << endl;
}
ms[1] += ms[2];
ms[1][1] = 100;
ms[1](1, 1) = 50;
cout << ms[1];
cout << "sum of m1=" << double(ms[1]);
double d = 1.2;
int i = int(d);
return 0;
}
6.2CMatrix.h
#pragma once
#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 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);
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;
}
6.3CMatrix.cpp
#include"CMatrix.h"
#include<fstream>
#include<assert.h>
CMatrix::CMatrix()
{
m_nRow = 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;
}
CMatrix::~CMatrix() {
Release();
}
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;
}
void CMatrix::Release() {
if (m_pData)
{
delete[]m_pData;
m_pData = NULL;
}
m_nRow = m_nCol = 0;
}
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 << 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;
}
CMatrix& CMatrix::operator=(const CMatrix& m)
{
if (this != &m) {
Create(m.m_nRow, m.m_nCol, m.m_pData);
}
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;
}
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 + nCol < m_nRow* m_nCol);
return m_pData[nRow * m_nCol + nCol];
}
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);
}
CMatrix::operator double()
{
double ds = 0;
for (int i = 0; i < m_nRow * m_nCol; i++)
{
ds += m_pData[i];
}
return ds;
}
7运行结果展示