栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

面向对象C++,运用move构造matrix类

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向对象C++,运用move构造matrix类

PROBLEM:

matrix.h + matrix.cpp

   Define a class Matrix to implement a concept – matrix of integers.

•The numbers of rows and columns are provided by the user of the class. In addition, define an exception type Matrix_exception , which is the inner class of Matrix.

NOTE:  Implementation Constraint:

•When choosing data representation, you cannot use any STL containers. Use low-level array, linked list, and others.
dot: move(),friend CODE:

matrix.cpp

#include
#include
#include
#include "matrix.h"
using namespace std;
Matrix::Matrix(int m=0,int n=0)
{
    a=new int [m*n];
    r=m;c=n;
}

Matrix::Matrix(const Matrix& b)
{
    cout<<"COPY"<     r=b.r;
    c=b.c;
    _init_a(b.a);
}

Matrix::Matrix(Matrix&& b)
{
    cout<<"MOVE COPY"<     r=b.r;c=b.c;
    a=b.a;
    b.r=0;b.c=0;
    b.a=nullptr;
}

Matrix::Matrix()
{
    r=0;c=0;
    a=new int[0];
}

Matrix::~Matrix()
{
    delete [] a;
}


int& Matrix::operator ()(int x,int y)
{
    return a[x*c+y];
}

int Matrix::col()
{
    return c;
}

int Matrix::row()
{
    return r;
}


Matrix Matrix::operator+(Matrix& b)
{
    if(r!=b.r||c!=b.c) throw Matrix_exception{};
    Matrix result{r,c};
    for(int i=0;i     {
        result.a[i]=a[i]+b.a[i];
    }
    return move(result);
}

Matrix Matrix::operator*(Matrix& b)
{
    if (c!=b.r) throw Matrix_exception{};
    Matrix result{r,b.c};
    for (int i=0;i     {
        for(int j=0;j         {
            int p=0;
            for (int k=0;k             {
                p+=a[i*c+k]*b.a[k*b.c+j];
            }
            result.a[i*result.c+j]=p;
        }
    }
    return move(result);
}

Matrix& Matrix::operator=(const Matrix& b)
{
    cout<<"COPY ASSIGN"<     r=b.r;c=b.c;
    delete [] a;
    a=new int[r*c];
    for (int i=0;i     return *this;
}


Matrix& Matrix::operator=(Matrix&& b)
{
    cout<<"MOVE ASSGIN"<     if(this!=&b)
    {
//        if(a) delete [] a;
        r=b.r;
        c=b.c;
        a=b.a;
        b.r=0;b.c=0;
        b.a=nullptr;
    }
    return *this;
}


ostream& operator<<(ostream& out,Matrix& b)

{
    for(int i=0;i     {
        for(int j =0;j         {
            out<         }
    }
    return out;
}

istream& operator>>(istream& in,const Matrix& b)
{
    for(int i=0;i     {
        for(int j=0;j             in>>b.a[i*b.c+j];
    }
    return in;
}
 

matrix.h

#ifndef RINGBUFFERQUEUE_H_INCLUDED
#define RINGBUFFERQUEUE_H_INCLUDED
#define MAX_N 100001
#include
#include
using namespace std;
class Matrix
{
private:
    int * a,r,c;      //r:行 c:列
    friend ostream& operator<<(ostream&,Matrix&);
    friend istream& operator>>(istream&,const Matrix&);


    void _init_a(const int* b)
    {
        a = new int[r*c];
        for(int i=0;i             a[i]=b[i];
    }
public:
    Matrix(int,int);
    ~Matrix();
    Matrix (Matrix&&);
    Matrix (const Matrix&);
    Matrix();
    int col();
    int row();
    int& operator()(int,int);
    Matrix operator*(Matrix&);
    Matrix operator+(Matrix&);
    Matrix& operator=(const Matrix&);
    Matrix& operator=(Matrix&&);
    struct Matrix_exception{};
};


#endif // RINGBUFFERQUEUE_H_INCLUDED


 

testMatrix.cpp

 // test program for Matrix:  testMatrix.cpp
#include
using namespace std;

#include "matrix.h"

int main(int argc,char**argv)
try
{
    int m,n;
    cout<<"Input the numbers of rows and columns:"<     cin>>m>>n;

    Matrix a{m,n},b{a},c;

    cout<<"Input the elements of Matrix:"<     for(int i=0; i         for(int j=0; j             cin>>a(i,j);
    cout<<"Input the elements of another Matrix:"<     for(int i=0; i         for(int j=0; j             cin>>b(i,j);
    cout<

    cout<<"Matrix A:";
    cout << "rows: " << a.row() << " columns: "
         << a.col() << endl << "data: " << endl << a << endl;
    cout<<"Matrix B:";
    cout << "rows: " << b.row() << " columns: "
         << b.col() << endl << "data: " << endl << b << endl;

    c=a+b;

    cout<<"Matrix A+B:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    c(0,0)*=100;
    c(1,1)=10000;
    cout << endl;
    cout<<"Matrix C:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    c=a*b;
    cout<<"Matrix A * B:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    Matrix d1;
    d1=c;

    Matrix d2{3,4};

    c=d1+d2;

   return 0;
}
catch(Matrix::Matrix_exception)
{
    cerr << "An exception happened in class Matrix"<     return 0;
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号