c++运用运算符重载和类实现矩阵相加,相减,相乘
下面展示 完整代码
。
#define LENGTH 20
#include <iostream>
#include<cstring>
using namespace std;
class Matrix {
public:
//构造器后面可以不用写大括号,但需要在类外面实现函数
Matrix();
Matrix(int row, int col);
Matrix(int* arr, int row, int col);
//矩阵相加
Matrix Add(const Matrix& om);
//以下几个函数实现矩阵+,-,*
Matrix operator+(const Matrix& om);
Matrix& operator=(const Matrix& om);
Matrix operator*(const Matrix& om);
Matrix operator-(const Matrix& om);
// 显示矩阵
void display();
//析构器
~Matrix()
{
//释放element指针
delete[]element;
};
//拷贝构造器
Matrix(const Matrix& om) {
row = om.row;
col = om.col;
//一定要分配空间
element = new int[row * col];
//此函数用于拷贝数组
memcpy(element, om.element, sizeof(int) * row * col);
};
friend ostream& operator<<(ostream&, const Matrix&);
protected:
//矩阵用一维数组存储
int* element;
int row;//控制行
int col;//控制列
};
//在外面实现符号重载函数,需类里面有定义
Matrix Matrix:: operator-(const Matrix& om)
{
//调用构造器,*this指针指向调用他的对象
Matrix ans(*this);
if (row == om.row && col == om.col) {
for (int i = 0; i < row * col; i++)
{
ans.element[i] -= om.element[i];
}
}
return ans;
}
Matrix Matrix:: operator*(const Matrix& om) {
// Matrix product();
//调用有参(两个参数)构造器
Matrix product(row, om.col);
if (col == om.row)
{
for (int r = 0; r < product.row; r++)
{
for (int c = 0; c < product.col; c++)
{
//将product矩阵置零
*(product.element + r * product.col + c) = 0;
for (int m = 0; m < this->col; m++)
{
//实现矩阵相乘
*(product.element + r * product.col + c) += *(this->element + r * col + m) * (*(om.element + m * om.col + c));
}
}
}
}
return product;
};
Matrix& Matrix::operator=(const Matrix& om) {
if (row != om.row || col != om.col) {
delete[]this->element;
element = new int[row * col];
row = om.row;
col = om.col;
}
memcpy(element, om.element, sizeof(int) * row * col);
return *this;
};
Matrix Matrix:: operator+(const Matrix& om) {
//调用构造器,*this指向调用他的对象(谁调用指向谁)
Matrix ans(*this);
if (row == om.row && col == om.col) {
for (int i = 0; i < row * col; i++)
{
ans.element[i] += om.element[i];
}
}
return ans;
}
//实现运算符<<重载输出矩阵
ostream& operator<<(ostream & os, const Matrix & om) {
os << endl;
for (int r = 0; r < om.row; r++)
{
os << "|";
for (int c = 0; c < om.col; c++)
{
cout << *(om.element + r * om.col + c) << " ";
}
os << "|" << endl;
}
// 需要返回一个引用类型的
return os;
};
//实现矩阵相加
Matrix Matrix::Add(const Matrix & om) {
Matrix ans(*this);
if (row == om.row &&col == om.col) {
for (int i = 0; i < row*col; i++)
{
ans.element[i] += om.element[i];
}
}
cout << "1";
return ans;
};
//实现类里面的无参构造器函数
Matrix::Matrix() {
// LENGTH 宏定义
row = LENGTH;
col = LENGTH;
element = new int[row * col];
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++)
{
*(element + r * col + c) = 0;
}
}
}
//实现类里面的有参构造器函数
Matrix::Matrix(int row, int col) {
element = new int[row * col];
this->col = col;
this->row=row;
/* memset() 是一个 C 语言中的库函数,用于将指定的内存块设置为指定的值。要在 C 代码中使用 memset() 函数,
你需要包含 <string.h> 或 <cstring> 头文件。
void *memset(void *ptr, int value, size_t num);
ptr:指向要设置值的内存块的指针。
value:要设置的值。value 和 ptr 指向的内存块的类型应兼容。一般情况下,可以使用整数或字符值作为 value。
num:要设置的字节数(或元素个数)。通常使用 sizeof() 运算符来确定要设置的字节数或元素个数
*/
memset(element, 1, sizeof(int) * row * col);
};
Matrix:: Matrix(int* arr,int row, int col) {
this->row = row;
this->col = col;
//引入#include<cstring>
element = new int[row*col];
/*
函数声明:void * memcpy ( void * destination, const void * source, size_t num );
参数
目的地:指向要在其中复制内容的目标数组的指针,类型转换为 void* 类型的指针。
源:指向要复制的数据源的指针,类型转换为 const void* 类型的指针。
数字:要复制的字节数。 size_t 是无符号整数类型。
头 文 件:#include <string.h>
返 回 值: 返回目的地。
*/
memcpy(element, arr, sizeof(int) * row * col);
}
// 实现显示矩阵
void Matrix::display() {
cout << endl;
for (int r = 0; r <row ; r++)
{
cout << "|";
for (int c = 0; c < col;c++)
{
//element数组首地址 r * col + c 指针偏移距离
cout << *(element + r * col + c) << "";
}
cout << "|" << endl;
}
}
int main() {
int arr1[4][5] = { {1,2,3,4,5},
{5,6,7,8,5},
{8,7,6,5,5},
{4,3,2,1,5} };
int arr2[5][4]
{
1,2,3,4,
5,5,6,7,
8,5,8,7,
6,5,5,4,
3,2,1,5,
};
Matrix m;
Matrix m1(arr1[0], 4, 5);
Matrix m6(arr2[0], 5, 4);
//此处调用拷贝构造器
Matrix m2=m1 ;
Matrix m3 = m1.Add(m2);
Matrix m4 = m3.operator+(m2);
Matrix m5 = m4 + m3;
Matrix m7 = m1 * m6;
Matrix m8 = m4 - m3;
//此处需要调用=运算符重载函数
m5 = m4 + m3;
cout << "m1" << m1 << "m2" << m2 << "m3" << m3<<"m4"<<m4;
cout << "m5" << m5 << "m6" << m6<<"m7"<<m7<<"m8"<<m8;
return 0;
}
效果图
因水平有限,还请多多指点