文章目录
一、代码展示
1.main.cpp
#include<iostream>
using namespace std;
#include"CMatrix.h"
#include "CComplex.h"
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);
cin >> m1;
m2.Set(1, 3, 10);
cout <<"m1 "<< m1 << "m2" << m2 << "m3" << m3 << "m4" << 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.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;
}
3.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("%lf%lf", &sc.m_dReal, &sc.m_dImag);
}
double CComplex::Modulus()
{
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("%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;
}
4.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 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;
}
#endif
5.CComplex.h
#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(const SComplex& sc);
class CComplex
{
public:
double m_dReal;
double m_dImag;
double Modulus();
void Output();
void Input();
};
istream& operator>>(istream& is, CComplex& cc);
ostream& operator<<(ostream& is, CComplex& cc);
#endif
二、运行结果
输入为1 1 3;
文本数据也为1 1 3;
三、总结
1.构造函数
(1)可以有多个构造函数进行构造,主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
(2)构造函数语法:类名(){}
a) 构造函数,没有返回值也不写void
b) 函数名称和类名相同
c) 构造函数可以有参数,因此可以发生重载
d) 程序在调用对象时候会自动调用构造,无须手动调用,并且只会调用一次
2.析构函数
(1)析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
(2)析构函数语法:~类名(){}
a) 析构函数,没有返回值也不写void
b) 函数名称与类名相同,在名称前加上符号~
c) 析构函数不可以有参数,因此不可以发生重载
d) 程序在对象销毁前会自动调用析构,无须手动调用,并且只会调用一次)
3.运算重载符
(1)对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
(2) 返回值用 类型+& 是为了避免做连续运算时候会出错.
(3) 转换函数默认返回类型为它本身
4.友元函数
(1)概念: 让一个函数或者类访问另一个类中私有成员,
(2)特殊关键字:friend,
(3)friend和operator同时使用可以让其他类对数据进行的操作符进行自定义