IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++(一) -> 正文阅读

[C++知识库]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;
}

所有函数的运行结果

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-08 11:36:46  更:2021-10-08 11:38:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 4:08:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码