定义Matrix类,为该类提供各种功能

定义Matrix类,为该类提供各种功能

定义Matrix类需要用到动态开辟二维数组

//动态开辟数组

 element = new int *[row];

    for (int i = 0; i < row; i++)
    {
    
    
        element[i] = new int[col];
    }

Matrix类的定义

class Matrix {
    
    
>     private:
>     int row, col;
>     int **element = NULL;
>     public:
>     static int number;
>     Matrix()//构造
>     {
    
    
>         cout<<"请输入矩阵的行数"<<endl;
>         cin>>row;
> 	    cout<<"请输入矩阵的列数"<<endl;
>         cin>>col;
> 
>     //动态开辟数组
>     element = new int *[row];
> 
>     for (int i = 0; i < row; i++)
>     {
    
    
>         element[i] = new int[col];
>     }
> 
>         Matrix::number++;
>     }
>     
>     Matrix (const Matrix &a)//拷贝构造
>     {
    
    
>         for(int i=0;i<a.row;i++)
>         {
    
    
>             for(int j=0;j<a.col;j++)
>             element[i][j]=a.element[i][j];
>         }
>     }
>     Matrix& operator=(const Matrix &rhs)//赋值
>     {
    
    
>         for(int i=0;i<rhs.row;i++)
>         {
    
    
>             for(int j=0;j<col;j++)
>             element[i][j]=rhs.element[i][j];
>         }
>         return *this;
>     }
>     Matrix operator+(const Matrix &b)//加法
>     {
    
    
>         Matrix temp;
>         for(int i=0;i<b.row;i++)
>         {
    
    
>             for(int j=0;j<b.col;j++)
>             temp.element[i][j]=this->element[i][j]+b.element[i][j];
>         }
>         return temp;
>     }
>     ~Matrix()
>     {
    
    
>          for (int i = 0; i < row; i++)
>     {
    
    
>         delete []element[i];
>         element[i] = NULL;
>     }
>     delete []element;
>     }
>     
>     friend Matrix operator-(const Matrix &a,const Matrix &b);
>     friend istream& operator>>(istream &cin,Matrix &rhs);
>     friend ostream& operator<<(ostream &cout,const Matrix &r); }; int Matrix::number=0;
>     Matrix operator-(const Matrix &a,const Matrix &b)//减法
>     {
    
    
>         Matrix temp;
>         for(int i=0;i<a.row;i++)
>         {
    
    
>             for(int j=0;j<a.col;j++)
>             temp.element[i][j]=a.element[i][j]-b.element[i][j];
>         }
>         return temp;
>     }
>     istream& operator>>(istream &cin,Matrix &rhs)
>     {
    
    
>         cout<<"请输入矩阵"<<endl;
> 
>     for (int i = 0; i < rhs.row; i++)
>     {
    
    
>         for (int j = 0; j < rhs.col; j++)
>         {
    
    
>             cin>>rhs.element[i][j];
>         }
>     }
>     return cin;
>     }
>     ostream& operator<<(ostream &cout,const Matrix &r)
>     {
    
    
>         //输出数组
>     cout<<"输出矩阵"<<endl;
> 
>     for (int i = 0; i < r.row; i++)
>     {
    
    
>         for (int j = 0; j < r.col; j++)
>         {
    
    
>             cout<<r.element[i][j];
>         }
>         cout<<endl;
>     }
>         return cout;
>     }
> 
> int main() {
    
    
>     Matrix a,b,c,d;
>     cin>>a;
>     cout<<a;
>     cin>>b;
>     cout<<b;
>     c=a+b;
>     cout<<c;
>     d=a-b;
>     cout<<d;
>     cout<<"number="<<Matrix::number<<endl; }

运行结果
在这里插入图片描述

参考资料:
1.https://blog.csdn.net/weixin_41966991/article/details/79994356

猜你喜欢

转载自blog.csdn.net/suxuanxuan/article/details/109560370
今日推荐