C++操作符重载

Matrix.h

#pragma once
#include "vector"
#include "iostream"
#define rep(i,n) for(int i=1;i<=n;i++) //宏定义for循环,精简代码
using namespace std;
class Matrix
{
public:
	//基本构造函数
	Matrix(int Row=0, int Column=0);
	//拷贝构造函数或者复制构造函数
	Matrix(const Matrix& matrix);
	//赋值操作符重载,必须为成员函数,不然会报错
    Matrix& operator=(const Matrix& matrix);
	//复合赋值操作符重载,建议重载为成员函数
	Matrix& operator+=(const Matrix& matrix);
	Matrix& operator*=(const Matrix& matrix);
	Matrix& operator*=(const float& number);
	Matrix& operator*=(const int& number);
	Matrix& operator-=(const Matrix& matrix);
	Matrix& operator/=(const float& number);
	float& operator[](const size_t& index);
	Matrix& operator++();//前缀式自增
	Matrix& operator--();//前缀式自减
	Matrix operator++(int); //后缀式自增
	Matrix operator--(int); //后缀式自减
	//算术和关系操作符一般为非成员函数,声明为友元
	friend Matrix operator+(const Matrix& matrix1, const Matrix& matrix2);
	friend Matrix operator-(const Matrix& matrix1, const Matrix& matrix2);
	friend Matrix operator*(const Matrix& matrix1, const Matrix& matrix2);
	friend Matrix operator*(const Matrix& matrix1, const float& number);
	friend Matrix operator*(const Matrix& matrix1, const int& number);
	friend bool operator==(const Matrix& matrix1, const Matrix& matrix2);
	friend bool operator!=(const Matrix& matrix1, const Matrix& matrix2);
	//输出操作符<<的重载,必须声明为友元
	friend ostream& operator<<(ostream& os, const Matrix&object);
	//输入操作符>>重载,必须声明为友元
	friend istream& operator >>(istream& in,Matrix&object);
	void Display();
	~Matrix();
public:
	int Row;
	int Column;
	vector<vector<float> > data; //二维vector,用于存放矩阵类数据
};



Matrix.cpp

#include "stdafx.h"
#include "Matrix.h"
#include "iomanip"
//构造函数
Matrix::Matrix(int Row/* =0 */, int Column/* =0 */){
	this->Row = Row;
	this->Column = Column;
	data.resize(Row + 1); //申请行数为row+1,0号位不用
	rep(i, Row)
		data[i].resize(Column + 1); //申请各行的列数
	rep(i, Row)
		rep(j, Column)
		data[i][j] = 0; //每个元素初始化为0,方便后面计算
}
//打印函数
void Matrix::Display(){
	rep(i, Row)
	{
		rep(j, Column)
			cout << setw(8) << data[i][j] << ' ';
		cout <<endl;
	}
}
//拷贝构造函数
Matrix::Matrix(const Matrix& matrix){
	Row = matrix.Row;
	Column = matrix.Column;
	data.resize(Row+1); //申请行数为row,0号位不用
	rep(i, Row)
		data[i].resize(Column+1); //申请各行的列数
	rep(i, Row)
		rep(j, Column)
		data[i][j] = matrix.data[i][j];
}
//赋值操作符重载
Matrix& Matrix::operator=(const Matrix& matrix){
	if (this==&matrix)
	{
		return *this;
	}
	Row = matrix.Row;
	Column = matrix.Column;
	//分配资源
	data.resize(Row + 1); //申请行数为row+1,0号位不用
	rep(i, Row)
		data[i].resize(Column + 1); //申请各行的列数
	rep(i, Row)
		rep(j, Column)
		data[i][j] = matrix.data[i][j];
	//返回本对象的引用
	return *this;
}
//复合赋值操作符重载
Matrix& Matrix::operator+=(const Matrix& matrix){
	if (Row == matrix.Row&&Column == matrix.Column)
	{
		rep(i, Row)
		{
			rep(j,Column)
			{
				data[i][j] += matrix.data[i][j];
			}
		}
	}
	return *this;
}
Matrix& Matrix::operator-=(const Matrix& matrix){
	if (Row == matrix.Row&&Column == matrix.Column)
	{
		rep(i, Row)
		{
			rep(j, Column)
			{
				data[i][j] -= matrix.data[i][j];
			}
		}
	}
	return *this;
}
Matrix& Matrix::operator*=(const float& number){
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] = data[i][j] * number;
		}
	}
	return *this;
}
Matrix& Matrix::operator*=(const int& number){
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] = data[i][j] * number;
		}
	}
	return *this;
}
Matrix& Matrix::operator*=(const Matrix& matrix){
	//先保存矩阵的值
	Matrix temp(Row, Column);
	rep(i, temp.Row)
	{
		rep(j, temp.Column)
		{
			temp.data[i][j] = data[i][j];
		}
	}
	//改变矩阵的大小和值
	Column = matrix.Column;
	data.clear();        //清除数据
	data.resize(Row+1); //申请行数为row+1,0号位不用
	rep(i, Row)
		data[i].resize(Column+1); //申请各行的列数
	//重新给矩阵赋值
	rep(i, temp.Row)
	{
		rep(j, matrix.Column)
		{
			double sum = 0;
			rep(k, temp.Column)
				sum += temp.data[i][k] * matrix.data[k][j];
			data[i][j] = sum; //改变矩阵的值
		}
	}
	return *this;
}
Matrix& Matrix::operator/=(const float& number){
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] = data[i][j] / number;
		}
	}
	return *this;
}
//前缀式自增
Matrix& Matrix::operator++(){
	//对每个元素都加1
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] +=1;
		}
	}
	return *this;
}
//前缀式自减
Matrix& Matrix::operator--(){
	//对每个元素都减1
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] -= 1;
		}
	}
	return *this;
}
//后缀式自增
Matrix Matrix::operator++(int){
	//拷贝构造函数
	Matrix ret(*this);
	//对每个元素都加1
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] += 1;
		}
	}
	return ret;
}
//后缀式自减
Matrix Matrix::operator--(int){
	//拷贝构造函数
	Matrix ret(*this);
	//对每个元素都减1
	rep(i, Row)
	{
		rep(j, Column)
		{
			data[i][j] -= 1;
		}
	}
	return ret;
}
//析构函数
Matrix::~Matrix()
{
	data.clear();
}
//加法操作符重载
Matrix operator+(const Matrix& matrix1, const Matrix& matrix2){
	Matrix temp(matrix1.Row, matrix1.Column);
	if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)
	{
		rep(i, matrix1.Row)
		{
			rep(j, matrix2.Column)
			{
				temp.data[i][j] = matrix1.data[i][j]+matrix2.data[i][j];
			}
		}
	}
	return  temp;
}
//减法操作符重载
Matrix operator-(const Matrix& matrix1, const Matrix& matrix2){
	Matrix temp(matrix1.Row, matrix1.Column);
	if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)
	{
		rep(i, matrix1.Row)
		{
			rep(j, matrix2.Column)
			{
				temp.data[i][j] = matrix1.data[i][j] - matrix2.data[i][j];
			}
		}
	}
	return  temp;
}
//乘法操作符重载
Matrix operator*(const Matrix& matrix1, const Matrix& matrix2){
	Matrix temp(matrix1.Row, matrix2.Column);
	rep(i, temp.Row)
	{
		rep(j, temp.Column)
		{
			double sum = 0;
			rep(k, matrix1.Column)
				sum += matrix1.data[i][k] * matrix2.data[k][j];
			temp.data[i][j] = sum;
		}
	}
	return temp;
}
//乘法操作符重载
Matrix operator*(const Matrix& matrix1, const float& number){
	Matrix temp(matrix1.Row, matrix1.Column);
	rep(i, temp.Row)
	{
		rep(j, temp.Column)
		{
			temp.data[i][j] = matrix1.data[i][j]* number;
		}
	}
	return temp;
}
//乘法操作符重载
Matrix operator*(const Matrix& matrix1, const int& number){
	Matrix temp(matrix1.Row, matrix1.Column);
	rep(i, temp.Row)
	{
		rep(j, temp.Column)
		{
			temp.data[i][j] = matrix1.data[i][j] * number;
		}
	}
	return temp;
}
//等于关系操作符重载
bool operator==(const Matrix& matrix1, const Matrix& matrix2){
	//只有维数相等才有可比性
	if (matrix1.Row==matrix2.Row&&matrix1.Column==matrix2.Column)
	{
		rep(i, matrix1.Row)
		{
			rep(j, matrix1.Column)
			{
				if (matrix1.data[i][j]!=matrix2.data[i][j])
				{
					return false;
				}
			}
		}
		return true;
	}
	else
	{
		return false;
	}	
}
//不等于关系操作符重载
bool operator!=(const Matrix& matrix1, const Matrix& matrix2){
	//只有维数相等才有可比性
	if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)
	{
		rep(i, matrix1.Row)
		{
			rep(j, matrix1.Column)
			{
				if (matrix1.data[i][j] != matrix2.data[i][j])
				{
					return true;
				}
			}
		}
		return false;
	}
	else
	{
		return false;
	}
}
//输出操作符重载
ostream& operator<<(ostream& os, const Matrix&object){

	rep(i, object.Row)
	{
		rep(j, object.Column)
			os << setw(8) << object.data[i][j] << ' ';
		os <<endl;
	}
	return os;
}
//输入操作符重载
istream& operator >>(istream& in, Matrix&object){
	rep(i, object.Row)
		rep(j, object.Column)
		in >> object.data[i][j];
	return in;
}

main.c

#include "iostream"
#include "Matrix.h"
using namespace std;
int main(){
	int row1, row2, col1, col2;
	cout << "请输入第一个矩阵的行和列:\n";
	cin >> row1 >> col1;
	Matrix m1(row1, col1);
	cout << "请输入" << row1 << '*' << col1 << "个数:\n";
	cin >> m1;
	cout << "输出矩阵的值:\n";
	cout << m1;
	cout << "请输入第二个矩阵的行和列:\n";
	cin >> row2 >> col2;
	Matrix m2(row2, col2);
	cout << "请输入" << row2 << '*' << col2 << "个数:\n ";
	cin >> m2;
	cout << "输出矩阵的值:\n";
	cout << m2;
	if (col1 != row2)
		cout << "这两个矩阵无法相乘\n";
	else
	{
		cout << "判断矩阵m1与m2是否相等:\n";
		if (m1==m2)
		{
			cout << "m1和m2相等:\n";
		}
		else
		{
			cout << "m1和m2不相等:\n";
		}

		cout << "m1拷贝构造m3矩阵结果输出:\n";
		Matrix m3(m1);
		cout << m3;

		cout << "m1赋值重载m4矩阵结果输出:\n";
		Matrix m4(m1.Row,m1.Column);
		m4 = m1;
		cout << m4;

		cout << "m1*m2矩阵相乘输出m5:\n";
		Matrix m5(m1.Row, m2.Column);
		m5 = m1*m2;
		cout << m5;

		cout << "矩阵m1*2输出m6:\n";
		Matrix m6(m1.Row, m1.Column);
		m6 = m1*2;
		cout << m6;

		cout << "矩阵m1*0.5输出m7:\n";
		Matrix m7(m1.Row, m1.Column);
		m7 = m1 * 0.5;
		cout << m7;

		cout << "m1*m2矩阵相乘输出m1:\n";
		m1 *= m2;
		cout << m1;

		cout << "m1矩阵前自增输出\n";
		cout << ++m1;

		cout << "m1矩阵后自增输出\n";
		cout << m1++;

		cout << "m1矩阵输出\n";
		cout << m1;

		cout << "m1矩阵前自减输出\n";
		cout << --m1;

		cout << "m1矩阵后自减输出\n";
		cout << m1--;

		cout << "m1矩阵输出\n";
		cout << m1;
	}
	return 0;
}
输出结果:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值