C++ 实验一 CMatrix类设计与实现

本文详细介绍了C++编程中CMatrix类的实现,包括构造函数、析构函数、运算符重载和友元函数的使用。通过实例展示了如何通过main.cpp、CMatrix.h和CMatrix.cpp文件来构建和操作矩阵。实验结果表明,CMatrix类成功实现了矩阵的创建、运算和友元函数的功能。
摘要由CSDN通过智能技术生成

一、代码实现

1.main.cpp

#include  <iostream>
#include <stdio.h>
#include "CMatrix.h"
using namespace std;
int main() {
    double pData[10]={2,3,4,5};
    CMatrix m1,m2(2,5,pData), m3("d:\\1.txt"),m4(m2);
    cin>>m1;
    m2.Set(1,3,10);
    cout<<m1<<m2<<m3<<m4;
    m4=m3;
    m4[2]=m4+1;
    if(m4==m3)
    {
        cout<<"Error !"<<endl;
    }
    m4 += m3;
    cout<<"sum of m4 = "<<(double)m4<<endl;
    return 0;
}

2.CMatrix.h

#pragma once
#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);
    double & operator[](int nIndex);
    double& operator()(int nRow,int nCol);
    bool operator==(const CMatrix& m);
    bool operator!=(co
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值