C++開發(fā)之實現(xiàn)操作符重載的實例-創(chuàng)新互聯(lián)

C++操作符重載

成都創(chuàng)新互聯(lián)10多年成都企業(yè)網(wǎng)站建設(shè)服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都企業(yè)網(wǎng)站建設(shè)及推廣,對混凝土攪拌罐等多個領(lǐng)域擁有多年的網(wǎng)站運維經(jīng)驗的網(wǎng)站建設(shè)公司。

實現(xiàn)效果圖:

C++ 開發(fā)之實現(xiàn)操作符重載的實例

實例代碼:

Matrix.h

#pragma once 
#include "vector" 
#include "iostream" 
#define rep(i,n) for(int i=1;i<=n;i++) //宏定義for循環(huán),精簡代碼 
using namespace std; 
class Matrix 
{ 
public: 
  //基本構(gòu)造函數(shù) 
  Matrix(int Row=0, int Column=0); 
  //拷貝構(gòu)造函數(shù)或者復(fù)制構(gòu)造函數(shù) 
  Matrix(const Matrix& matrix); 
  //賦值操作符重載,必須為成員函數(shù),不然會報錯 
  Matrix& operator=(const Matrix& matrix); 
  //復(fù)合賦值操作符重載,建議重載為成員函數(shù) 
  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); //后綴式自減 
  //算術(shù)和關(guān)系操作符一般為非成員函數(shù),聲明為友元 
  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,用于存放矩陣類數(shù)據(jù) 
};

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁標(biāo)題:C++開發(fā)之實現(xiàn)操作符重載的實例-創(chuàng)新互聯(lián)
地址分享:http://www.muchs.cn/article22/epojc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計電子商務(wù)、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)