栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实验一之CMatrix类设计与实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实验一之CMatrix类设计与实现

一、代码设计 1.1 构造函数

CMatrix(): 不带参数的构造函数;
CMatrix(int nRow, int nCol, double *pData=NULL) : 带行、列及数据指针等参数的构造函数,并且参数带默认值;
CMatrix(const char * strPath): 带文件路径参数的构造函数;
CMatrix(const CMatrix& m): 拷贝构造函数
此外会用列表初始化成员变量:CMatrix(): m_nRow(0), m_nCol(0), m_pData(NULL);
bool Create(int nRow, int nCol, double *pData=NULL): 先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。

1.2 析构函数

~CMatrix(): 调用Release();
Release(): 将内存释放,并将行列设置为0;

1.3 运算符重载

算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理

1.4 友元函数

输入和输出运输符:<<, >>

二、代码实现 2.1 CMatrix.h
#pragma once
#include 
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;
}
2.2 CMatrix.cpp
#include "CMatrix.h"
#include 
#include 
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;
}
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;
}
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 < 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;
}
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;
}
2.3 main.cpp
#include  < iostream>
#include 
#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[0] = m4 + 1;
    if (m4 == m3)
    {
        cout << "Error !" << endl;
    }
    m4 += m3;
    cout << "sum of m4 = " << (double)m4 << endl;
    return 0;
}
2.4 运行结果

输入1 1 2
1.txt 1 1 1

三、总结 3.1 构造函数与析构函数
  • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
  • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
3.2 运算重载符

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

加号运算符重载

作用:实现两个自定义数据类型相加的运算

左移运算符重载

作用:可以输出自定义数据类型

递增运算符重载

作用: 通过重载递增运算符,实现自己的整型数据

赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

关系运算符重载

**作用:**重载关系运算符,可以让两个自定义类型对象进行对比操作

函数调用运算符重载
  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
3.3 友元函数

友元的目的就是让一个函数或者类 访问另一个类中私有成员

友元的关键字为 friend

友元的三种实现

  • 全局函数做友元
  • 类做友元
  • 成员函数做友元
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号