目录

一、实验内容

二、代码实现

1、main.cpp

2、CMatrix.h

3、CMatrix.cpp

4、文本文件 1.txt

三、运行结果

四、代码分析

1、构造函数

2、析构函数

3、运算符重载

4、友元函数


一、实验内容

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中。
2、析构函数
~CMatrix(): 调用Release();
Release(): 将内存释放,并将行列设置为0;
3、运算符重载
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理
4、友元函数
输入和输出运输符:<<, >>

二、代码实现

1、main.cpp

#include <iostream>
//#include "CComplex.h"
#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);cin>>m1;m2.Set(1,3,10);cout<<m1<<m2<<m3<<m4;m4=m3;m4[2]=m4+1;if(m4==m3){cout<<"Error !"<<endl;}m4 += m3;cout<<"sum of m4 = "<<(double)m4<<endl;return 0;
}

2、CMatrix.h

#pragma once
#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

3、CMatrix.cpp

#include "CMatrix.h"
#include <fstream>
#include <assert.h>
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));}
}
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& 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 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;
}
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;
}

4、文本文件 1.txt

三、运行结果

四、代码分析

1、构造函数

​
CMatrix();
//不带参数的构造函数CMatrix(int nRow, int nCol, double* pData = NULL);
//带有行、列及数据指针等参数的构造函数,并且参数带有默认值CMatrix(const CMatrix& m);
//带有文件路径参数的构造函数CMatrix(const char* strPath);
//拷贝构造函数​

1、构造函数的主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用

2、构造函数语法:类名(){}
        a) 构造函数,没有返回值也不写void
        b) 函数名称和类名相同
        c) 构造函数可以有参数,因此可以发生重载
        d) 程序在调用对象时候会自动调用构造,无须手动调用,并且只会调用一次

2、析构函数

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

1、析构函数的主要作用在于对象销毁前系统自动调用,执行一些清理工作

2、析构函数语法:~类名(){}
        a) 析构函数,没有返回值也不写void
        b) 函数名称与类名相同,在名称前加上符号~
        c) 析构函数不可以有参数,因此不可以发生重载
        d) 程序在对象销毁前会自动调用析构,无须手动调用,并且只会调用一次

3、运算符重载

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

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

2、本次实验中主要对:
        a)算术运算符重载:+, -, +=, -=,
        b)关系运算符重载:>, <, ==
        c)下标操作符:[], ()
        d)强制类型转换: double
        e)赋值运算符:=

4、友元函数

    friend istream& operator>>(istream& is, CMatrix& m);friend ostream& operator<<(ostream& os, const CMatrix& m);

友元函数是让一个函数或者类访问另一个类中私有成员的函数,特殊关键字:friend

C++ [实验一] CMatrix类设计与实现相关推荐

  1. C++程序设计:实验一 CMatrix类设计与实现

    前言 在实验一当中,主要内容为CMatrix类的设计与实现,包含了CMatrix.h,CMatrix.cpp以及main.cpp三大文件. 文章目录 前言 一.实验要求: 1.构造函数 2.析构函数 ...

  2. [C++学习实验1]CMatrix类设计与实现

    一.头文件创建 1.头文件定义了CMatrix类.类內部属性.函数.以及各种调用方法 头文件定义格式如下,属于条件编译宏定义,可以根据条件选择性的只编译某段程序,也可以防止重复定义. #ifndef ...

  3. C++学习记录 实验1 CMatrix类设计与实现

    目录 一.源码实现 1.1 Main.cpp 1.2 CMatrix.cpp 1.3 CMatrix.h 二.知识小结 2.1 构造函数和析构函数 2.1.1 构造函数和析构函数的由来 2.1.2 构 ...

  4. C++-实验一 CMatrix类设计

    文章目录 前言 一.代码部分 1.Main函数 2.CMatrix.h 3.CMatrix.cpp 二.运行结果 三.总结 一. 构造和析构函数: 二. 运算符重载: 三. 友元函数: 前言 争取一周 ...

  5. 实验1 CMatrix类设计与实现

    一.头文件的创建 C++对于创建一个类以及各项方法的推荐做法是在头文件.h中将类中所有的函数,变量预先定义,之后再在另一个.cpp文件中将这些所有方法一一实现,所以我们需要将我们对于矩阵类(我们之后都 ...

  6. C++ ——CMatrix类设计与实现

    CMatrix类设计与实现 实验内容: 一.构造函数 1.CMatrix(): 不带参数的构造函数: 2.CMatrix(int nRow, int nCol, double *pData=NULL) ...

  7. C++ CMatrix类设计与实现

    实验一:CMatrix类设计与实现 一 代码实现 1.main.cpp 2.CMatrix.h 3.CMatrix.cpp 二 运行截图 三 总结 1.构造函数 2.析构函数 3.运算符重载 4.友元 ...

  8. CMatrix类设计

    文章目录 前言 一.实验内容 二.实现代码 1.main.cpp 2.CMatrix.h 3.CMatrix.h 4.运行结果 总结 前言 通过对CMatrix类的设计熟悉c++的类与对象,多态,构造 ...

  9. C++实验(一)—— CMatrix类设计与实现

    目录 一.代码理解 CMatrix.h CMatrix.cpp Main.cpp 运行结果 二.总结 一.代码理解 CMatrix.h #ifndef CMATRIX_H //防止被重复引用 #def ...

最新文章

  1. 新版蚂蚁网有抄袭怪兽吗?
  2. uni-app中的页面间使用$on与$emit
  3. java语法结构是什么意思_java - 基础 - 语法结构
  4. 获取本机主机名称和IP地址
  5. SAP FI配置关键点
  6. CodeForces 1204 (#581 div 2)
  7. 跟开涛学 SpringMVC
  8. 分享一套高级Java笔试题(实拍高清图)
  9. c++ -- 重载、重写(覆盖)和隐藏的区别
  10. 专题一 关于Windows10安装问题
  11. 比鲁大师好的测试电脑软件,电脑跑分软件哪个好?好用的电脑跑分软件盘点
  12. 导数乘法/除法法则的证明
  13. NameError: name ‘xxx‘ is not defined问题总结
  14. 【数据恢复案例】.[decrypt20@firemail.cc].eking新型变种勒索病毒
  15. 错误:Attempted read from closed stream尝试读取关闭的流!!!
  16. 发送邮件提示Sender address rejected: not owned by user错误
  17. Java获取URL对应的资源
  18. 柔性机械臂_少年,这儿有个“超柔软”的6轴机械臂求带走
  19. julia语言和python_后起之秀!Julia胜于Python的5个优势
  20. Webfrom --中国直辖市三区联动

热门文章

  1. STM32H750 SRAM中下载和调试程序
  2. I2C读取EEROM时序图
  3. CVS 常用操作命令
  4. 一般哪个企业邮箱好用
  5. 安卓应用开发Socket通信 客户端+服务器端
  6. pic32mx是什么cpu_Digilent Microchip PIC32MX795F512 32位MCU开发方案
  7. 菜鸟教程java的list_Java菜鸟教程
  8. 月入5万的程序员,日子过得就像月薪5000
  9. Go 每日一库之 testify
  10. Fusion360学习记录:螺丝螺帽