C++编程练习 矩阵模板类的实现

C++编程练习 矩阵模板类的实现

前言

通过自定义模板实现了一个矩阵及其运算的Matrix类,并重载了+ - * += -= *= [] 等运算符,但是没有实现列表初始化方式的构造函数。

本文编写的代码可以用来学习使用,涉及到的知识点有模板、构造函数、拷贝构造、虚函数、动态内存分配、运算符重载等等,对初学C++的朋友来说是极好的练手。

代码编写过程中借鉴了https://blog.csdn.net/m0_37772174/article/details/83018940的代码,这篇文章中还实现了矩阵的行变换、高斯消元等运算。

UML图

UML图如下。
UML

代码

#pragma once
#include<vector>
#include<iostream>
using namespace std;

template<typename T>
class Matrix
{
public:
	//Constructors
	Matrix();
	Matrix(int row, int column);
	Matrix(int row, int column, T val);
	Matrix(Matrix const& secondMatrix);
	virtual ~Matrix();   //Destructor shoule be declared as virtual unless it won't be inheritanced

	//Basic operation
	int getRow() const;
	int getColumn() const;
	T getValue(int x, int y) const;
	void setValue(int x, int y,T val);
	void show();
	
	//Math operation
	Matrix add(Matrix const& secondMatrix) const;
	Matrix subtract(Matrix const& secondMatrix) const;
	Matrix multiply(Matrix const& secondMatrix) const;

	//Operator overloading
	Matrix& operator=(Matrix const& secondMatrix);
	Matrix operator+(Matrix const& secondMatrix) const;
	Matrix operator-(Matrix const& secondMatrix) const;
	Matrix operator*(Matrix const& secondMatrix) const;
	Matrix operator+=(Matrix const& secondMatrix);
	Matrix operator-=(Matrix const& secondMatrix);
	Matrix operator*=(Matrix const& secondMatrix);
	T* operator[](int x) const;

private:
	int row;
	int column;
	T **value;

	void initialize();     //Initialize Matrix & allocate memory
};

/********************************************************************************/

template<typename T>
Matrix<T>::Matrix() {
	this->row = 1;
	this->column = 1;
	initialize();
	value[0][0] = 0;
}

template<typename T>
Matrix<T>::Matrix(int row, int column) {
	this->row = row;
	this->column = column;
	initialize();
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			value[i][j] = 0;
		}
	}
}

template<typename T>
Matrix<T>::Matrix(int row, int column, T val) {
	this->row = row;
	this->column = column;
	initialize();
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			value[i][j] = val;
		}
	}
}

template<typename T>
Matrix<T>::Matrix(Matrix const& secondMatrix) {
	this->row = secondMatrix.getRow();
	this->column = secondMatrix.getColumn();
	initialize();
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			value[i][j] = secondMatrix.getValue(i, j);
		}
	}
}

template<typename T>
Matrix<T>::~Matrix() {
	for (int i = 0; i < row; i++) {
		delete[] value[i];
	}
	delete[] value;
}

template<typename T>
void Matrix<T>::initialize() {
	value = new T * [row];
	for (int i = 0; i < row; i++) {
		value[i] = new T[column];
	}
}

template<typename T>
int Matrix<T>::getRow() const {
	return this->row;
}

template<typename T>
int Matrix<T>::getColumn() const {
	return this->column;
}

template<typename T>
T Matrix<T>::getValue(int x, int y) const {
	return value[x][y];
}

template<typename T>
void Matrix<T>::setValue(int x, int y, T val) {
	value[x][y] = val;
}

template<typename T>
void Matrix<T>::show() {
	for (int i = 0; i < getRow(); i++) {
		for (int j = 0; j < getColumn(); j++) {
			cout << getValue(i, j) << ",";
		}
		cout << endl;
	}
	cout << endl;
}

template<typename T>
Matrix<T> Matrix<T>::add(Matrix const& secondMatrix) const {
	Matrix result(row, column);
	if (secondMatrix.getRow() != getRow()
		|| secondMatrix.getColumn() != getColumn()) {
		cout << "Sizes of Two Matrix are not equal! " << endl;
		return *this;
	}
	for (int i = 0; i < getRow(); i++) {
		for (int j = 0; j < getColumn(); j++) {
			result.setValue(i, j, value[i][j] + secondMatrix.getValue(i, j));
		}
	}
	return result;
}

template<typename T>
Matrix<T> Matrix<T>::subtract(Matrix const& secondMatrix) const {
	Matrix result(row, column);
	if (secondMatrix.getRow() != getRow()
		|| secondMatrix.getColumn() != getColumn()) {
		cout << "Sizes of Two Matrix are not equal! " << endl;
		return *this;
	}
	for (int i = 0; i < getRow(); i++) {
		for (int j = 0; j < getColumn(); j++) {
			result.setValue(i, j, value[i][j] - secondMatrix.getValue(i, j));
		}
	}
	return result;
}

template<typename T>
Matrix<T> Matrix<T>::multiply(Matrix const& secondMatrix) const {
	Matrix result(row, secondMatrix.getColumn(), 0);
	if (getColumn() != secondMatrix.getRow()) {
		cout << "Wrong Format! " << endl;
		return *this;
	}
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < secondMatrix.getColumn(); j++) {
			T sum = 0;
			for (int k = 0; k < row; k++) {
				sum += value[i][k] * secondMatrix.getValue(k, j);
			}
			result.setValue(i, j, sum);
		}
	}
	return result;
}

template<typename T>
Matrix<T>& Matrix<T>::operator=(Matrix const& secondMatrix) {
	if (this == &secondMatrix)
		return *this;
	if (secondMatrix.getRow() != getRow()
		|| secondMatrix.getColumn() != getColumn()) {
		for (int i = 0; i < row; i++) {
			delete[] value[i];
		}
		delete[] value;

		row = secondMatrix.getRow();
		column = secondMatrix.getColumn();
		initialize();
	}

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			value[i][j] = secondMatrix.getValue(i, j);
		}
	}
	return *this;
}

template<typename T>
Matrix<T> Matrix<T>::operator+(Matrix const& secondMatrix) const {
	return add(secondMatrix);
}

template<typename T>
Matrix<T> Matrix<T>::operator-(Matrix const& secondMatrix) const {
	return subtract(secondMatrix);
}

template<typename T>
Matrix<T> Matrix<T>::operator*(Matrix const& secondMatrix) const {
	return multiply(secondMatrix);
}

template<typename T>
Matrix<T> Matrix<T>::operator+=(Matrix const& secondMatrix) {
	*this = add(secondMatrix);
	return *this;
}

template<typename T>
Matrix<T> Matrix<T>::operator-=(Matrix const& secondMatrix) {
	*this = subtract(secondMatrix);
	return *this;
}

template<typename T>
Matrix<T> Matrix<T>::operator*=(Matrix const& secondMatrix) {
	*this = multiply(secondMatrix);
	return *this;
}

template<typename T>
T* Matrix<T>::operator[](int x) const {
	return value[x];
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值