实验1 CMatrix类设计与实现

一、头文件的创建

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

#ifndef CMATRIX_H
#define CMATRIX_H
#include <iostream>
#include <string.h>
using namespace std;
class CMatrix
{
public:
    CMatrix();
    CMatrix(int nRow,int nCol,double *pData=NULL);
    CMatrix(const char * strPath);
    CMatrix(const CMatrix & m);
    ~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);

    double & operator[](int nIndex);
    double & operator()(int nRow,int nCol)
    {
        return m_pData[nRow*m_nCol+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

二、各类构造器的创建

接下来我们都是在CMatrix.cpp文件下实现这些方法,先引用一下头文件

#include "cmatrix.h"
#inclu
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值