C++(一)

普通构造函数类

  1. CMatrix()
    没有参数的函数构造,函数源码为:
CMatrix::CMatrix() :m_nRow(0), m_nCol(0), m_pData(0){

}

另一种构造方式为:

CMstrix::CMatrix(){
	m_nRow=0;
	m_nCol=0;
	m_pData=0;
}
  1. CMatrix(int nRow, int nCol, double* pData = NULL)
    有参数的构造函数,构造时可以知道行数、列数以及矩阵内的具体值
    注:m_pData(0)与m_pData= NULL等价
CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0) {
	
	Create(nRow, nCol, pData);
}
  1. CMatrix(const CMatrix& m);
    构造一个函数,将括号内的矩阵拷贝至括号外
    有三种不同的构造方法
CMatrix::CMatrix(const CMatrix& m) : m_pData(0) {
	*this = m;
}
CMatrix::CMatrix(const CMatrix& m) {
	  m_nRow = m.m_nRow;
	  m_nCol = m.m_nCol;
	  m_pData = m.NULL;
	  Create(m_nRow,m_nCol,m.m_npData);
}
CMatrix::CMatrix(const CMatrix& m) : CMatrix(m.m_nRow,m.m_nCil,m_npData) {

}
  1. CMatrix(const char * strPath):文件流读取函数
  • 注:this:成员函数中都有this指针,指向本身对象的地址
    this指针加变成对象
    this指针加&变成地址
CMatrix::CMatrix(const char* strPath) {
	m_pData = 0;
	m_nRow = m_nCol = 0;
	ifstream cin(strPath);//读入文本
	cin >> *this;
}

  1. ~CMatrix():析构函数
    主要用于删除内存时使用,既当某一函数为虚构函数时,删除其内存
    release()函数将在下文提到,本质是删除内存语句
CMatrix::~CMatrix() {//虚构时删除内存
	Release();
}

其他的构造函数

  1. 创建函数:bool Create(int nRow, int nCol, double* Data = NULL)
bool CMatrix::Create(int nRow, int nCol, double* pData) {
	Release();
	m_pData = new double[nRow * nCol];
	m_nRow = nRow;
	m_nCol = nCol;
	if (pData) {
		return memcpy(m_pData, pData, nRow * nCol * sizeof(double));
	}
}
  1. 修改矩阵值:void Set(int nRow, int nCol, double dVale)
    此函数的作用为指定修改将第几行第几列的值修改为赋予的值
    因为函数内部语句很少所以使用内联函数提高效率
    内联函数的规格应该不超过十行,并且递归、循环函数不能作为内联函数
//显示内联函数写法
inline void CMatrix::Set(int nRow, int nCol, double dVal) {
	m_pData[nRow * m_nCol + nCol] = dVal;
}
//隐式内联函数写法:定义后直接实现
void CMatrix::Set(int nRow, int nCol, double dVal) {
		m_pData[nRow * m_nCol + nCol] = dVal;
}
  1. 释放内存:void Release()
void CMatrix::Release() {
	if (m_pData) {
		delete[]m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;

}

输入输出流的重构

加上friend后两个函数可以放在内部,依然是全局函数而不是成员函数,只是赋予了改函数访问所有变量的特权

friend istream & operator>>(istream& is, CMatrix& m);
friend ostream& operator<<(ostream& os, const CMatrix& m);
  1. 输出:istream & operator>>(istream& is, CMatrix& m)
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;
}
  1. 输出:ostream& operator<<(ostream& os, const CMatrix& m)
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;//换行\n
	}
	return os;
}

运算符重构

  1. 赋值:CMatrix& operator = (const CMatrix& m)
CMatrix& CMatrix::operator=(const CMatrix& m) {
	if (this != &m) {
		Create(m.m_nRow, m.m_nCol, m.m_pData);
	}
	return *this;
}
  1. +=:CMatrix& operator += (const CMatrix& m)
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;
}
  1. 判断是否等于:bool operator == (const CMatrix& m)
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;
}
  1. 不等于: bool operator != (const CMatrix& m)
bool CMatrix::operator!=(const CMatrix& m) {
	return !((*this) == m);
}
  1. 下标:double& operator[](int nIndex)
double& CMatrix::operator[](int nIndex) {
	assert(nIndex < m_nRow * m_nCol);
	return m_pData[nIndex];
}
  1. 圆括号:double& operator()(int nRow, int nCol)
    可以输入无数的值
double& CMatrix::operator()(int nRow,int nCol) {
	assert(nRow*m_nCol+nCol<m_nRow*m_nCol);
	return m_pData[nRow*m_nCol+nCol];
}
  1. 加号:CMatrix operator+(const CMatrix& m1, const CMatrix& m2);
    此函数为全局函数
CMatrix operator+(const CMatrix& m1, const CMatrix& m2) {
	CMatrix m3(m1);
	m3 += m2;
	return m3;
}

强制转换函数

operator double()

CMatrix::operator double() {
	double ds = 0;//返回值
	for (int i = 0; i < m_nRow * m_nCol; i++) {
		ds += m_pData[i];
	}
	return ds;
}

主函数源码

#include <iostream>
#include "CComplex.h"
#include<stdio.h>
#include"CMatrix.h"
using namespace std;

int main(int argc, char** argv) {

	CMatrix m1;//无参构造m1,输出
	cout << "无参构造m1 :" << m1 << endl;

	double pData[10] = { 2,3,4,5 };
	CMatrix m2(2, 5, pData);//构造m2,2行5列,赋值
	cout <<"有参构造m2: "<< m2  << endl;

	CMatrix m3("d://1.txt");//读取文件流
	cout << "读取文件流m3: " << m3 << endl;

	CMatrix m4(m2);
	cout << "拷贝复制m4 :" << m4 << endl;

	m2.Set(0, 2, 10);
	cout << "修改后m2 :" << m4 << endl;

	m1 = m2;
	cout << "修改后m1 :" << m4 << endl;

	if (m4 == m2) {
		cout << "Erroe!" << endl;
	}

	m4 += m1;
	cout << "sum of m4 = " << m4 << endl;

	cout << "强制转换 m4=" << (double)m4 << endl;
	
	return 0;
}

所有函数的运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值