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

本文档详细介绍了CMatrix类的设计,包括CMatrix.h和CMatrix.cpp的实现,以及Main.cpp的使用。重点讲解了C++中的命名空间std、友元函数、构造器、重载运算符和内联函数的应用。实验过程中,还涉及到CMD编码设置以避免中文乱码问题。通过此次实验,作者深化了对C++构造器、类和语言特性的理解,尤其是析构函数和内联函数的运用。
摘要由CSDN通过智能技术生成

目录

 

CMatrix.h

CMatrix.cpp

Main.cpp


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

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

CMatrix operator+(const CMatrix& m1, const CMatrix& m2);
CMatrix operator-(const CMatrix& m1, const CMatrix& m2);

inline void CMatrix::Set(int nRow, int nCol, double dVal)
{
    m_pData[nRow * m_nCol + nCol + nCol] = dVal;
}

#endif // !CMATRIX_H

CMatrix.cpp

#include"CMatrix.h"
#include<fstream>
#include<assert.h>

//对类成员进行初始化  无参构造器
// 无参构造器:
CMatrix::CMatrix() :m_nRow(0), m_nCol(0), m_pData(0)
{

}
//带行、列及数据指针等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值