C++ 開發(fā)之實(shí)現(xiàn)操作符重載的實(shí)例
更新時(shí)間:2017年07月19日 14:28:58 作者:liuyi1207164339
這篇文章主要介紹了C++ 開發(fā)之實(shí)現(xiàn)操作符重載的實(shí)例的相關(guān)資料,這里附有實(shí)例代碼和實(shí)現(xiàn)效果圖幫助大家參考實(shí)踐,需要的朋友可以參考下
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<vector<float> > data; //二維vector,用于存放矩陣類數(shù)據(jù)
};
Matrix.cpp
#include "stdafx.h"
#include "Matrix.h"
#include "iomanip"
//構(gòu)造函數(shù)
Matrix::Matrix(int Row/* =0 */, int Column/* =0 */){
this->Row = Row;
this->Column = Column;
data.resize(Row + 1); //申請(qǐng)行數(shù)為row+1,0號(hào)位不用
rep(i, Row)
data[i].resize(Column + 1); //申請(qǐng)各行的列數(shù)
rep(i, Row)
rep(j, Column)
data[i][j] = 0; //每個(gè)元素初始化為0,方便后面計(jì)算
}
//打印函數(shù)
void Matrix::Display(){
rep(i, Row)
{
rep(j, Column)
cout << setw(8) << data[i][j] << ' ';
cout <<endl;
}
}
//拷貝構(gòu)造函數(shù)
Matrix::Matrix(const Matrix& matrix){
Row = matrix.Row;
Column = matrix.Column;
data.resize(Row+1); //申請(qǐng)行數(shù)為row,0號(hào)位不用
rep(i, Row)
data[i].resize(Column+1); //申請(qǐng)各行的列數(shù)
rep(i, Row)
rep(j, 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); //申請(qǐng)行數(shù)為row+1,0號(hào)位不用
rep(i, Row)
data[i].resize(Column + 1); //申請(qǐng)各行的列數(shù)
rep(i, Row)
rep(j, Column)
data[i][j] = matrix.data[i][j];
//返回本對(duì)象的引用
return *this;
}
//復(fù)合賦值操作符重載
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, Row)
{
rep(j, Column)
{
data[i][j] -= matrix.data[i][j];
}
}
}
return *this;
}
Matrix& Matrix::operator*=(const float& number){
rep(i, Row)
{
rep(j, Column)
{
data[i][j] = data[i][j] * number;
}
}
return *this;
}
Matrix& Matrix::operator*=(const int& number){
rep(i, Row)
{
rep(j, 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(); //清除數(shù)據(jù)
data.resize(Row+1); //申請(qǐng)行數(shù)為row+1,0號(hào)位不用
rep(i, Row)
data[i].resize(Column+1); //申請(qǐng)各行的列數(shù)
//重新給矩陣賦值
rep(i, temp.Row)
{
rep(j, 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, Row)
{
rep(j, Column)
{
data[i][j] = data[i][j] / number;
}
}
return *this;
}
//前綴式自增
Matrix& Matrix::operator++(){
//對(duì)每個(gè)元素都加1
rep(i, Row)
{
rep(j, Column)
{
data[i][j] +=1;
}
}
return *this;
}
//前綴式自減
Matrix& Matrix::operator--(){
//對(duì)每個(gè)元素都減1
rep(i, Row)
{
rep(j, Column)
{
data[i][j] -= 1;
}
}
return *this;
}
//后綴式自增
Matrix Matrix::operator++(int){
//拷貝構(gòu)造函數(shù)
Matrix ret(*this);
//對(duì)每個(gè)元素都加1
rep(i, Row)
{
rep(j, Column)
{
data[i][j] += 1;
}
}
return ret;
}
//后綴式自減
Matrix Matrix::operator--(int){
//拷貝構(gòu)造函數(shù)
Matrix ret(*this);
//對(duì)每個(gè)元素都減1
rep(i, Row)
{
rep(j, Column)
{
data[i][j] -= 1;
}
}
return ret;
}
//析構(gòu)函數(shù)
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, 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, const Matrix& matrix2){
Matrix temp(matrix1.Row, matrix2.Column);
rep(i, temp.Row)
{
rep(j, 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.Row)
{
rep(j, 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, matrix1.Column);
rep(i, temp.Row)
{
rep(j, temp.Column)
{
temp.data[i][j] = matrix1.data[i][j] * number;
}
}
return temp;
}
//等于關(guān)系操作符重載
bool operator==(const Matrix& matrix1, const Matrix& matrix2){
//只有維數(shù)相等才有可比性
if (matrix1.Row==matrix2.Row&&matrix1.Column==matrix2.Column)
{
rep(i, matrix1.Row)
{
rep(j, matrix1.Column)
{
if (matrix1.data[i][j]!=matrix2.data[i][j])
{
return false;
}
}
}
return true;
}
else
{
return false;
}
}
//不等于關(guān)系操作符重載
bool operator!=(const Matrix& matrix1, const Matrix& matrix2){
//只有維數(shù)相等才有可比性
if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)
{
rep(i, matrix1.Row)
{
rep(j, 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;
}
main.c
#include "iostream"
#include "Matrix.h"
using namespace std;
int main(){
int row1, row2, col1, col2;
cout << "請(qǐng)輸入第一個(gè)矩陣的行和列:\n";
cin >> row1 >> col1;
Matrix m1(row1, col1);
cout << "請(qǐng)輸入" << row1 << '*' << col1 << "個(gè)數(shù):\n";
cin >> m1;
cout << "輸出矩陣的值:\n";
cout << m1;
cout << "請(qǐng)輸入第二個(gè)矩陣的行和列:\n";
cin >> row2 >> col2;
Matrix m2(row2, col2);
cout << "請(qǐng)輸入" << row2 << '*' << col2 << "個(gè)數(shù):\n ";
cin >> m2;
cout << "輸出矩陣的值:\n";
cout << m2;
if (col1 != row2)
cout << "這兩個(gè)矩陣無法相乘\n";
else
{
cout << "判斷矩陣m1與m2是否相等:\n";
if (m1==m2)
{
cout << "m1和m2相等:\n";
}
else
{
cout << "m1和m2不相等:\n";
}
cout << "m1拷貝構(gòu)造m3矩陣結(jié)果輸出:\n";
Matrix m3(m1);
cout << m3;
cout << "m1賦值重載m4矩陣結(jié)果輸出:\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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
DEV?C++源碼編譯后控制臺(tái)輸出中文亂碼問題解決
本文主要介紹了DEV?C++源碼編譯后控制臺(tái)輸出中文亂碼問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
C++實(shí)現(xiàn)將圖片轉(zhuǎn)換為馬賽克效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了C++如何實(shí)現(xiàn)將圖片轉(zhuǎn)換為馬賽克效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-01-01
C/C++讀取大文件數(shù)據(jù)方式詳細(xì)講解
這篇文章主要介紹了C語言/C++讀取大文件數(shù)據(jù)的完整方式過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
C語言庫函數(shù)qsort及bsearch快速排序算法使用解析
這篇文章主要為大家介紹了C語言庫函數(shù)qsort及bsearch快速排序算法的使用示例解析2022-02-02

