栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 开发之实现操作符重载的实例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 开发之实现操作符重载的实例

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 > data; //二维vector,用于存放矩阵类数据 
}; 

Matrix.cpp

#include "stdafx.h" 
#include "Matrix.h" 
#include "iomanip" 
//构造函数 
Matrix::Matrix(int Row, int Column){ 
  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 <>(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; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/63422.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号