十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
C++操作符重載
實(shí)現(xiàn)效果圖:
實(shí)例代碼:
Matrix.h
#pragma once #include "vector" #include "iostream" #define rep(i,n) for(int i=1;i<=n;i++) //宏定義for循環(huán),精簡(jiǎ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ù),不然會(huì)報(bào)錯(cuò) 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> data; //二維vector,用于存放矩陣類數(shù)據(jù) };
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。