C++ 开发之实现操作符重载的实例
更新时间:2020年4月25日 17:30 点击:1621
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; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: C语言动态内存分配的详解
下一篇: C语言实现“幸运数”的实例详解
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 本篇文章是对C#中方法重载进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- 这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
- 这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25