头文件
CMatrix.h
#pragma once
#ifndef CMATRIX_H
#define CMATRIX_H
#include<iostream>//定义标准输入输出流对象,该头文件没有定义全局命名空间
using namespace std;//命名空间std内定义的所有标识符都有效,std也是所有标识符的定义空间
//类CMatrix定义
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);
//CMatrix& operator+(const CMatrix& m);
//Cmatrix operator+(const CMatrix& m1,const CMatrix& m2);
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;
}
#endif
以下代码用于防止多次引用造成的编译错误
#ifndef CMATRIX.H
#define CMATRIX.H
......
#endif
对应函数定义
CMatrix.cpp
//""用于在本目录下查找文件,<>用于在系统目录下查找文件
#include"CMatrix.h"
#include<fstream>//文件操作函数
#include<assert.h>//断言宏定义,用于计算现表达式,若错误则终止程序运行
//无参构造,初始化对象并定义变量
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;
}
//析构函数,调用释放资源函数
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;
}
//* 内存复制,将pData的数据以nRow * nCol * sizeof(double)的字节数复制到m_pData中
//资源释放函数,将类中的数据资源释放
void CMatrix::Release()
{
if (m_pData)
{
delete[]m_pData;
m_pData = NULL;
}
m_nRow = m_nCol = 0;
}
//运算符=重载的构造函数
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& 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& 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;
}
//运算符[]重载
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];
}
//运算符==重载
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;
}
//友元函数重载>>操作符
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;
}
CComplex.h
#pragma once
#ifndef CCOMPLEX_H
#define CCOMPLEX_H
#include<iostream>
using namespace std;
struct SComplex
{
double m_dReal;
double m_dImag;
};
double Modulus(const SComplex& sc);
void Output(const SComplex& sc);
void Input(SComplex& sc);
class CComplex
{
public:
double m_dReal;
double m_dImag;
double Moudulus();
void Output();
void Input();
};
istream& operator>>(istream& is, CComplex& cc);
ostream& operator<<(ostream& os, const CComplex& cc);
#endif
CComplex.cpp
#include"CComplex.h"
#include<math.h>
#include<stdio.h>
double Modulus(const SComplex& sc)
{
return sqrt(sc.m_dReal * sc.m_dReal + sc.m_dImag * sc.m_dImag);
}
void Output(const SComplex& sc)
{
printf("%f %f\n", sc.m_dReal, sc.m_dImag);
}
void Input(SComplex& sc)
{
scanf_s("%lf%lf", &sc.m_dReal, &sc.m_dImag);
}
double CComplex::Moudulus()
{
return sqrt(m_dReal * m_dReal + m_dImag * m_dImag);
}
void CComplex::Output()
{
printf("%f %f\n", m_dReal, m_dImag);
}
void CComplex::Input()
{
scanf_s("%lf%lf", &m_dReal, & m_dImag);
}
istream& operator>>(istream& is, CComplex& cc)
{
is >> cc.m_dReal >> cc.m_dImag;
return is;
}
ostream& operator<<(ostream& cout, const CComplex& cc)
{
cout << cc.m_dReal << " " << cc.m_dImag << endl;
return cout;
}
Main.cpp
#include<iostream>
/* run this program using the console pauser or add your own getch,system("pause")*/
#include"CComplex.h"
#include"CMatrix.h"
#include<stdio.h>
using namespace std;
int main(int argc, char** argv) {
double pData[10] = { 2,3,4,5 };
CMatrix m1, m2(2, 5, pData), m3("d:\\1.txt"), m4(m2);//用4种方式构造类
cin >> m1;//命令输入值
m2.Set(1, 3, 10);
cout << m1 << m2 << m3 << m4;//输出4个数据
m4 = m3;//=操作符重载使用
m4[2] = m4 + 1;
if (m4 == m3)//==操作符重载使用
{
cout << "Error!" << endl;
}
m4 += m3;
cout << "sun of m4 = " << (double)m4 << endl;
return 0;
}
文件d:\\1.txt的数据:
2 2 1 2 3 4
代码运行实例: