ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

CMatrix类设计与实现(C++第一次实验)

2021-10-12 22:06:05  阅读:185  来源: 互联网

标签:nRow CMatrix int pData C++ nCol 实验 构造函数


CMatrix类设计与实现(C++第一次实验)

1、构造函数:

1.1 构造函数的概念

相对于C语言来说,C++有一个比较好的特性就是构造函数,即类通过一个或者几个特殊的成员函数来控制其对象的初始化过程。构造函数的任务,就是初始化对象的数据成员,无论何时只要类的对象被创建,就会执行构造函数。

1.2 CMatrix(): 不带参数的构造函数

代码:

 CMatrix::CMatrix() : m_nRow(0), m_nCol(0), m_pData(0)  //无参构造函数 
 {
	
 }

需要注意的是CMatrix::CMatrix() : m_nRow(0), m_nCol(0), m_pData(0)和CMatrix(){
m_nRow=0,m_nCol=0,m_pData=0}效果一致,但是前一种相对构建速度会快一点。

1.3 CMatrix(int nRow, int nCol, double *pData=NULL) : 带行、列及数据指针等参数的构造函数,并且参数带默认值

代码:

 CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0) // 带有三个参数的构造函数 
 {
	     Create(nRow, nCol, pData);  //调用新建类对象方法
 }

1.4 CMatrix(const char * strPath): 带文件路径参数的构造函数

 CMatrix::CMatrix(const char* strPath)
 {
	     m_pData = 0;
	     m_nRow = m_nCol = 0;
	     ifstream cin(strPath);
	     cin >> *this;
 }

注意:
ofstream: 写操作(输出)的文件类 (由ostream引申而来)
ifstream: 读操作(输入)的文件类(由istream引申而来)
fstream: 可同时读写操作的文件类 (由iostream引申而来)
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间

1.5 CMatrix(const CMatrix& m): 拷贝构造函数

代码:

 CMatrix::CMatrix(const CMatrix & m) : m_pData(0) //带有一个参数为对象的构造函数 
 {
	     *this = m;
 }

什么是拷贝构建函数呢?
复制构造函数是构造函数的一种,也称拷贝构造函数,它只有一个参数,参数类型是本类的引用。
复制构造函数的参数可以是 const 引用,也可以是非 const 引用。
一般使用前者,这样既能以常量对象(初始化后值不能改变的对象)作为参数,也能以非常量对象作为参数去初始化其他对象。一个类中写两个复制构造函数,一个的参数是 const 引用,另一个的参数是非 const 引用,也是可以的。

2、析构函数

概念

析构函数(destructor)是成员函数的一种,它的名字与类名相同,但前面要加~,没有参数和返回值。

一个类有且仅有一个析构函数。如果定义类时没写析构函数,则编译器生成默认析构函数。如果定义了析构函数,则编译器不生成默认析构函数。

析构函数在对象消亡时即自动被调用。可以定义析构函数在对象消亡前做善后工作。

2.1 ~CMatrix(): 调用Release();

 CMatrix::~CMatrix()
 {
	     Release();
 }
 

3、CMatrix对象方法

3.1 对象初始化:bool CMatrix::Create(int nRow, int nCol, double* pData)

 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));  
		         //memcpy内存拷贝函数,从pData中拷贝nRow * nCol * sizeof(double)个字节的内容到m_pData中 
		 }
 }

3.2 对象销毁:Release()

void CMatrix::Release()
 {
	     if (m_pData)
		 {
		         delete[]m_pData;  //内存释放
		         m_pData = NULL;
		  }
	 m_nRow = m_nCol = 0;   //将行列设置为0
 }

对象销毁函数Release()其实就是将内存释放,并将行列设置为0。

4、运算符重载

4.1 算术运算符重载:+, -, +=, -=

代码:

CMatrix & CMatrix::operator+=(const CMatrix & m)   //+=运算符重载
 {
	   assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
	  //assert断言函数,对括号内的假设进行判断,假如不符合条件就抛出错误,终止程序运行
	   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;
 }
 CMatrix operator-(const CMatrix& m1, const CMatrix& m2)   //-运算符重载 
{
		CMatrix m3(m1);
		m3 -= m2;
		return m3;
}
CMatrix& CMatrix::operator-=(const CMatrix& m)  //-= 运算符重载 
{
		//assert断言函数,对括号内的假设进行判断,假如不符合条件就抛出错误,终止程序运行
		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;
}

4.2 关系运算符重载:>, <, ==

代码:

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

4.3 下标操作符:[], ()的重载

代码:

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

4.4 强制类型转换: double

代码:

CMatrix::operator double()  //重载强制类型转换
{
	double dS = 0;
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		dS += m_pData[i];
	}
	return dS;
}

4.5 赋值运算符:=

代码:

CMatrix& CMatrix::operator=(const CMatrix& m)   //=运算符的重载
{
	if (this != &m)
	{
		//“=”赋值采用Create方法,是深拷贝
		Create(m.m_nRow, m.m_nCol, m.m_pData);
	}
	return *this;
}

5、友元函数

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

代码:

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

6、主函数main.cpp以及头文件CMatrix.h

main.cpp:

#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("C:\\Users\\Administrator\\Desktop\\C++\\第一次实验\\测试文本.txt"),m4(m2);
     cin>>m1;
     m2.Set(1,3,10);
     cout<<m1<<m2<<m3<<m4;
     m4=m3;
     m4[2]=m4+1;
     if(m4==m3)
     {
         cout<<"出错了!"<<endl;
     }
     m4 += m3;
     cout<<"m4的和 = "<<(double)m4<<endl;
     return 0;
 }

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);
         //  CMatrix& operator+(const CMatrix& m);
         //  CMatrix operator+(const CMatrix& m1,const CMatrix& m2);
             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

7、实验运行结果及总结

在这里插入图片描述
复制构造函数是构造函数的一种,也称拷贝构造函数,它只有一个参数,参数类型是本类的引用。
复制构造函数的参数可以是 const 引用,也可以是非 const 引用。
一般使用前者,这样既能以常量对象(初始化后值不能改变的对象)作为参数,也能以非常量对象作为参数去初始化其他对象。一个类中写两个复制构造函数,一个的参数是 const 引用,另一个的参数是非 const 引用,也是可以的。

析构函数(destructor)是成员函数的一种,它的名字与类名相同,但前面要加~,没有参数和返回值。
一个类有且仅有一个析构函数。如果定义类时没写析构函数,则编译器生成默认析构函数。如果定义了析构函数,则编译器不生成默认析构函数。
析构函数在对象消亡时即自动被调用。可以定义析构函数在对象消亡前做善后工作。

ofstream: 写操作(输出)的文件类 (由ostream引申而来)
ifstream: 读操作(输入)的文件类(由istream引申而来)
fstream: 可同时读写操作的文件类 (由iostream引申而来)
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间

标签:nRow,CMatrix,int,pData,C++,nCol,实验,构造函数
来源: https://blog.csdn.net/Scracs/article/details/120711692

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有