c++中矩阵相乘(对象数组与对象指针的应用)
- 设计一个矩阵类 Matrix(矩阵由二维数组实现),有分配空间和对矩阵赋值的功能。
- 将这个矩阵类的对象作为参数传送到函数 Mul()
用三种方式传递参数:
直接传送:Mul(Matrix a,Matrix b),实际上只是传送值,在函数中针对 对象的任何修改均不影响该对象本身
指针传送:Mul(Matrix *pa,Matrix *pb)
引用传送:Mul(Matrix &a,Matrix &b)将 Mul() 函数实现
- 定义一个友元函数实现转制功能。转制是指将数组中 a[i][j]与 a[j][i]的值对调。
下面给出代码
#include<iostream>
using namespace std;
class Matrix { //定义类
private:
int **pmatrix;
int imaxx,imaxy;
static int matrixnum; //计算有多少个矩阵
public:
Matrix(unsigned int x,unsigned int y);
Matrix(Matrix &m);
~Matrix();
void SetValue(unsigned int x,unsigned int y,int value);
void input();
void print() const;
friend Matrix Rotate(Matrix &m); //定义友元函数实现转置功能
friend Matrix Mul(Matrix a,Matrix b); //定义MUL函数,实现矩阵相乘的功能
unsigned int getx(){