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

本文档详细介绍了在VSCode环境下进行C++实验,涉及构造函数(无参数、有参数、拷贝构造、地址构造)、析构函数的使用。重点讲解了运算符重载,包括输入输出、算术、赋值和关系运算符的重载。通过实验,加深了对C++语法结构、函数多态和重载的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验背景

本次实验基于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&c1,<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值