C++ CMatrix类设计与实现
实验一:CMatrix类设计与实现
- 一 代码实现
- 1.main.cpp
- 2.CMatrix.h
- 3.CMatrix.cpp
- 二 运行截图
- 三 总结
- 1.构造函数
- 2.析构函数
- 3.运算符重载
- 4.友元函数
一 代码实现
1.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("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.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
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;
}
二 运行截图
输入1 1 3
文件1.txt中内容也为 1 1 3
三 总结
1.构造函数
CMatrix();CMatrix(int nRow, int nCol, double* pData=NULL);CMatrix(const CMatrix& m);CMatrix(const char* strPath);
1.可以有多个构造函数进行构造(可以重载)
2.double* pData=NULL 无pData传入时默认进行赋值null
2.析构函数
~CMatrix();
1 析构函数不可以有参数,因此不可以发生重载
2.程序在对象销毁前会自动调用析构,无须手动调用,并且只会调用一次
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 返回值用 类型+& 是为了避免做连续运算时候会出错.原因就是没加&你返回的不是地址而是被Copy了一个临时对象
3 转换函数默认返回类型为它本身
4.友元函数
friend istream & operator>>(istream& is, CMatrix& m);friend ostream & operator<<(ostream & os, const CMatrix & m);
1.在类中声明友元函数使其可以访问类的私有数据
2.friend和operator同时使用可以让其他类对数据进行的操作符进行自定义
C++ CMatrix类设计与实现相关推荐
- C++ ——CMatrix类设计与实现
CMatrix类设计与实现 实验内容: 一.构造函数 1.CMatrix(): 不带参数的构造函数: 2.CMatrix(int nRow, int nCol, double *pData=NULL) ...
- CMatrix类设计
文章目录 前言 一.实验内容 二.实现代码 1.main.cpp 2.CMatrix.h 3.CMatrix.h 4.运行结果 总结 前言 通过对CMatrix类的设计熟悉c++的类与对象,多态,构造 ...
- C++程序设计:实验一 CMatrix类设计与实现
前言 在实验一当中,主要内容为CMatrix类的设计与实现,包含了CMatrix.h,CMatrix.cpp以及main.cpp三大文件. 文章目录 前言 一.实验要求: 1.构造函数 2.析构函数 ...
- [C++学习实验1]CMatrix类设计与实现
一.头文件创建 1.头文件定义了CMatrix类.类內部属性.函数.以及各种调用方法 头文件定义格式如下,属于条件编译宏定义,可以根据条件选择性的只编译某段程序,也可以防止重复定义. #ifndef ...
- C++学习记录 实验1 CMatrix类设计与实现
目录 一.源码实现 1.1 Main.cpp 1.2 CMatrix.cpp 1.3 CMatrix.h 二.知识小结 2.1 构造函数和析构函数 2.1.1 构造函数和析构函数的由来 2.1.2 构 ...
- C++实验(一)—— CMatrix类设计与实现
目录 一.代码理解 CMatrix.h CMatrix.cpp Main.cpp 运行结果 二.总结 一.代码理解 CMatrix.h #ifndef CMATRIX_H //防止被重复引用 #def ...
- C++实验一之CMatrix类设计与实现
一.代码设计 1.1 构造函数 CMatrix(): 不带参数的构造函数: CMatrix(int nRow, int nCol, double *pData=NULL) : 带行.列及数据指针等 ...
- C++ [实验一] CMatrix类设计与实现
目录 一.实验内容 二.代码实现 1.main.cpp 2.CMatrix.h 3.CMatrix.cpp 4.文本文件 1.txt 三.运行结果 四.代码分析 1.构造函数 2.析构函数 3.运算符 ...
- 实验1 CMatrix类设计与实现
一.头文件的创建 C++对于创建一个类以及各项方法的推荐做法是在头文件.h中将类中所有的函数,变量预先定义,之后再在另一个.cpp文件中将这些所有方法一一实现,所以我们需要将我们对于矩阵类(我们之后都 ...
最新文章
- IDC机房:运营商机房与第三方机房(昌平机房)优劣对比
- JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
- LVS入门篇(二)之LVS基础
- 关于Android 传感器坐标与读数的进一步讨论
- 顺便抹了下眼眶的飞鸽传书官方网站
- Intel张旭:通信和计算的融合将是5G的关键
- select 的操作
- 字典树实现_trie 字典树的实现方法
- 在Qt工程中调用GmSSL
- Arduino下LCD1602综合探究(下)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法
- vbox安装深度linux,win10系统上用VirtualBox虚拟机安装国产深度deepin系统教程
- php ctf题,CTF---PHP安全考题
- Codeforces 1253B Silly Mistake
- DIY成本35的自制蓝牙音响
- 编译原理(二)文法和语言、符号和符号串、文法的类型、语法树
- 航空公司VIP客户查询 (25 分)
- 80核处理器_最受欢迎的处理器 酷睿i5-9400F果然霸榜了
- 货币金融学-期末复习
- 本地python版GEE安装及配置步骤(jupyter lab)
- 修道士与野人问题——C++源代码,伪代码,详细分析
热门文章
- 新目标大学英语综合教程2_课后答案
- 黑客攻防学习笔记 准备工作
- android百度输入法源码,Android内置第三方输入法
- CSDN高校——C1任务01—修改游戏存档
- ftp服务器一直显示登录密码,ftp一直弹出用户名密码
- Java项目:SSM驾校预约管理系统
- 大叔分几种类型_大叔真心爱上你的表现有哪些 这几个细节别忽略
- 网页403是怎么回事?网页403的原因和解析。
- 国标GB/T28181流媒体服务器EasyGBS新版研发——设备信息清零问题
- brew 一直等待_去广告神器,一键去除所有主流App广告,看片从此不等待