C++ 开发之实现操作符重载的实例
发布时间:2020-12-30 17:12:09 所属栏目:经验 来源:网络整理
导读: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,i
副标题[/!--empirenews.page--]
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 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,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,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,Column) { data[i][j] -= matrix.data[i][j]; } } } return *this; } Matrix& Matrix::operator*=(const float& number){ rep(i,Column) { data[i][j] = data[i][j] * number; } } return *this; } Matrix& Matrix::operator*=(const int& number){ rep(i,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,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,Column) { data[i][j] = data[i][j] / number; } } return *this; } //前缀式自增 Matrix& Matrix::operator++(){ //对每个元素都加1 rep(i,Column) { data[i][j] +=1; } } return *this; } //前缀式自减 Matrix& Matrix::operator--(){ //对每个元素都减1 rep(i,Column) { data[i][j] -= 1; } } return *this; } //后缀式自增 Matrix Matrix::operator++(int){ //拷贝构造函数 Matrix ret(*this); //对每个元素都加1 rep(i,Column) { data[i][j] += 1; } } return ret; } //后缀式自减 Matrix Matrix::operator--(int){ //拷贝构造函数 Matrix ret(*this); //对每个元素都减1 rep(i,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,matrix2.Column) { temp.data[i][j] = matrix1.data[i][j] - matrix2.data[i][j]; } } } return temp; } //乘法操作符重载 Matrix operator*(const Matrix& matrix1,matrix2.Column); rep(i,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.Column) { temp.data[i][j] = matrix1.data[i][j]* number; } } return temp; } //乘法操作符重载 Matrix operator*(const Matrix& matrix1,const int& number){ Matrix temp(matrix1.Row,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.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.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; } (编辑:淮安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |