一、代码实现
1.main.cpp
#include <iostream>
#include <stdio.h>
#include "CMatrix.h"
using namespace std;
int main() {
double pData[10]={2,3,4,5};
CMatrix m1,m2(2,5,pData), m3("d:\\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;
}
2.CMatrix.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!=(co