C++操作符重載

實現(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:
//基本構造函數(shù)
Matrix(int Row=0, int Column=0);
//拷貝構造函數(shù)或者復制構造函數(shù)
Matrix(const Matrix& matrix);
//賦值操作符重載,必須為成員函數(shù),不然會報錯
Matrix& operator=(const Matrix& matrix);
//復合賦值操作符重載,建議重載為成員函數(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ù),聲明為友元
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ù)
};另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁標題:C++開發(fā)之實現(xiàn)操作符重載的實例-創(chuàng)新互聯(lián)
地址分享:http://chinadenli.net/article22/epojc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、手機網(wǎng)站建設、網(wǎng)站設計、電子商務、外貿(mào)網(wǎng)站建設、響應式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容