C++ 开发之实现操作符重载的实例
发布时间:2020-12-30 21:36:30 所属栏目:创业 来源:网络整理
导读: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; } (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |