C++实验_1:函数构造与运算符重载

文章目录实验背景1.函数解析1.1构造函数1.1.1无参数构造函数1.1.2有参数构造函数1.1.3拷贝构造函数1.1.4地址构造函数1.2析构函数2.运算符重载2.1输入输出运算符重载2.1.1 输入运算符重载2.1.2输出运算符重载2.2 算术运算符重载2.3赋值运算符重载2.4关系运算符重载实验背景本次实验基于vscode下的c++环境,实现了关于CMatrix系列函数和运算符的重载1.函数解析#ifndef CMATRIX_H#define CMATRIX_H#include<io
摘要由CSDN通过智能技术生成

实验背景

本次实验基于vscode下的c++环境,实现了关于CMatrix系列函数和运算符的重载

1.函数解析

#ifndef CMATRIX_H
#define CMATRIX_H
#include<iostream>
#include<string.h>
using namespace std;

//二维数组
class CMatrix
{
   
    //类属性
    private:
    //行数
    int m_nRow;
    //列数
    int m_nCol;
    //数据
    double *m_pData=NULL;

    public:
    //默认构造函数
    CMatrix();
    //带行,列,数据指针参数的构造函数
    CMatrix(int nRow,int nCol,double *pData=NULL);
    //拷贝构造函数
    CMatrix(const CMatrix &cc);
    //带文件路径的构造函数
    CMatrix(const char*strPath);
    //释放内存的析构函数,调用Release();
    ~CMatrix();
    void Release();

    //创建空间,令分配空间和实际空间匹配
    bool Create(int nRow,int nCol,double *pData=NULL);
    //释放内存
    //修改数据值;内联函数,提高效率(代码尽量简洁);循环,递归最好不要写成内联
    void Set(int nRow,int nCol,double dVall)
    {
   
        m_pData[nRow*m_nCol+nCol]=dVall;
    }
    //友元函数,授权声明,赋予访问权限
    friend istream& operator>>(istream& is,CMatrix&cc);
    friend ostream& operator<<(ostream& os,const CMatrix&cc);

    //运算符重载:+,+=,-,-=;
    CMatrix& operator +=(const CMatrix&cc);
    CMatrix& operator -=(const CMatrix&cc);

    //下标操作符:[],()
    double & operator[](int nIndex);
    double & operator()(int nRow,int nCol);

    //强制类型转换:double
    operator double();

    //赋值运算符:=
    CMatrix& operator =(const CMatrix&cc);

    //关系运算符重载,>,<,==,!=
    bool operator ==(const CMatrix&cc);
    bool operator !=(const CMatrix&cc);
    bool operator >(const CMatrix&cc);
    bool operator <(const CMatrix&cc);
};
CMatrix operator -(const CMatrix&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值