C++实验一——CMatirx类设计与实现

目录

一、构造函数

二、析构函数

三、运算符重载

四、友元函数

五、运行结果


一、构造函数

        函数声明:

    CMatrix();  //不带参数的构造函数//
    CMatrix(int nRow, int nCol, double *pData = NULL);  //带行、列及数据指针等参数的构造函数,并且参数带默认值//
    CMatrix(const CMatrix& m);  //拷贝构造函数//
    CMatrix (const char * strPath);  //带文件路径参数的构造函数//
    bool Create(int nRow, int nCol, double *pData = NULL);  //先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中//
    CMatrix& operator = (const CMatrix& m);
    CMatrix& operator += (const CMatrix& m);
    CMatrix& operator -= (const CMatrix& m);
    bool operator == (const CMatrix& m);
    double & operator[](int nIndex);
    double & operator()(int nRow, int nCol);
    friend istream & operator>>(istream& is, CMatrix & m);
    friend ostream & operator<<(ostream& os, const CMatrix& m);

         函数构造:

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;
}   //带文件路径参数的构造函数//

二、析构函数

        析构函数在对象消亡时即自动被调用。可以定义析构函数在对象消亡前做善后工作。在这个例子中,对象消亡时自动调用析构函数,执行Release(),释放内存并且将行列设置为0。

CMatrix::~CMatrix(){
    Release();  //将内存释放,并将行列设置为0//
}   //调用Release()//

三、运算符重载

        运算符重载,就是对已有的运算符重新进行定义,赋予其一种功能,以适应不同的数据类型。在这个例子中,重载运算符来实现矩阵之间的运算。

        算数运算符重载(+=, -=,+,-):

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

        关系运算符(>, <, ==):

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

        下标操作符([], ()):

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];
}

        强制类型转换(double):

CMatrix::operator double(){
    double dS = 0;
    for(int i = 0; i < m_nRow * m_nCol; i++){
        dS += m_pData[i];
    }
    return dS;
}

        赋值运算符(=):

CMatrix& CMatrix::operator = (const CMatrix& m){
    if(this != &m){
        Create(m.m_nRow, m.m_nCol, m.m_pData);
    }
    return *this;
}

四、友元函数

        类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。在这个例子中,我们使用友元函数来重载输入和输出符号,实现矩阵的输入和输出。

        输入和输出运输符(<<,>>):

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值