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++设计:关于CMatrix类的相关操作 -> 正文阅读

[C++知识库]C++设计:关于CMatrix类的相关操作

1.头文件声明

#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

2.函数的实现

? ? ? ? ? ? ? (1)构造器。 本次实验提供四个构造器,下面四个构造器分别为无参构造器,有参构造器,已经复制已有的类创建新的类,通过文件读入构造。

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;
}

? ? ? ? (2)下面为对象的创建,删除和析构函数。析构函数为当对象结束其生命周期,系统自动执行析构函数。例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存

bool CMatrix::Create(int nRow, int nCol, double* pData)
{
	Release();
	m_pData = new double[nRow * nCol];
	m_nRow = nRow;
	m_nCol = nCol;
	if (pData != NULL)
	{
		memcpy(m_pData, pData, nRow * nCol * sizeof(double));
		return true;
	}
	else {
		return false;
	}

}
void CMatrix::Release()
{
	if (m_pData!=NULL)
	{
		delete []m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;
}
CMatrix::~CMatrix()
{
	Release();
}

? ? ? ? (3)运算符重载:下面分别对‘=’,‘+=’,‘+’,‘==’,‘!=’进行重载

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;
}
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);
}

? ? ? ? (4)操作符重载:下面分别对‘[]’,‘()’,‘<<’,‘>>'进行重载

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];
}
istream& operator>>(istream& is, CMatrix& m)
{
	is >> m.m_nRow >> m.m_nCol;
	m.Create(m.m_nRow, m.m_nCol,m.m_pData);
	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;
}

? ? ? ? (5)数据类型转换,将创建的对象强制转换成double类

CMatrix::operator double()
{
	double dS = 0;
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		dS += m_pData[i];
	}
	return dS;
}

3.主函数

#include <iostream>
#include <stdio.h>
#include "CMatrix.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);
	cout << "输入矩阵行,列和对于数量的数值:" << endl;
	cin >> m1;
	m2.Set(1, 3, 10);
	cout << "m1=" << m1 << "\nm2=" << m2 << "\nm3=" << m3 << "\nm4=" << m4;
	m4 = m3;
	m4[2] = m4 + 1;
	if (m4 == m3)
	{
		cout << "Error !" << endl;
	}
	m4 += m3;
	cout << "\nm4=" << m4 << endl;
	cout << "sum of m4 = " << (double)m4 << endl;
	return 0;
}

4.结果展示

?5.总结

? ? ? ? 本次实验涉及到类的创建,删除,已经运算符的重载,对类的强制类型转换等等。还有析构函数的使用。本次实验编写明白很多项目构建的问题,其中对于内敛函数,无参函数让系统更快运行也有了深刻影响。对于各类重载方法有了大致的了解。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-11 17:20:07  更:2021-10-11 17:20:43 
 
开发: 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/19 23:48:47-

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