C++運(yùn)算符重載實(shí)例代碼詳解(調(diào)試環(huán)境 Visual Studio 2019)
最近看了菜鳥教程里的C++教程
遇到很多運(yùn)算符重載,為了方便我的學(xué)習(xí),我把這些總結(jié)了一下
如有錯(cuò)誤(包括之前的博文)請(qǐng)?jiān)u論留言,謝謝!
由于代碼里注釋的很清楚,我就不做過(guò)多的解釋了。
下面是這次總結(jié)的頭文件,用來(lái)放置一些類和方法。
//C++運(yùn)算符重載實(shí)例.h #pragma once #include <iostream> using namespace std; class chongzai { private: int i, j, k; public: chongzai() { i = 0; j = 0; k = 0; } chongzai(int a, int b, int c) { i = a; j = b; k = c; } //以下分別為A1,A2,A3的顯示函數(shù) void display1() { cout << "A1:" << endl; cout << "i=" << i << endl; cout << "j=" << j << endl; cout << "k=" << k << endl; cout << "------------------" << endl; } void display2() { cout << "A2:" << endl; cout << "i=" << i << endl; cout << "j=" << j << endl; cout << "k=" << k << endl; cout << "------------------" << endl; } void display3() { cout << "A3:" << endl; cout << "i=" << i << endl; cout << "j=" << j << endl; cout << "k=" << k << endl; cout << "------------------" << endl; } /*************************************************************************************************/ /* 一元運(yùn)算符重載: 遞增運(yùn)算符( ++ )和遞減運(yùn)算符( -- ) 一元減運(yùn)算符,即負(fù)號(hào)( - ) 邏輯非運(yùn)算符( ! ) */ chongzai operator-() //以負(fù)號(hào)( - )為例 { i = -i; j = -j; k = -k; return chongzai(i, j, k); } /*************************************************************************************************/ /* 二元運(yùn)算符重載: 二元運(yùn)算符需要兩個(gè)參數(shù) 加運(yùn)算符( + ),減運(yùn)算符( - ),乘運(yùn)算符( * )和除運(yùn)算符( / ) */ chongzai operator+(const chongzai& n) //以加號(hào)( + )為例 { chongzai A; A.i = this->i + n.i; A.j = this->j + n.j; A.k = this->k + n.k; return A; } /**********************************************************************************************/ /* 關(guān)系運(yùn)算符重載: 大于( > ),小于( < ),大于等于( >= ),小于等于( <= ),等于( = )等 */ bool operator<(const chongzai& n) //以小于號(hào)( < )為例 { if (i < n.i) return true; if (i >= n.i) return false; return false; } /**********************************************************************************************/ /* 輸入輸出運(yùn)算符重載: 流提取運(yùn)算符 >> 和流插入運(yùn)算符 << 使用友元函數(shù)無(wú)需設(shè)置對(duì)象,而且符合人們cout<<和cin>>的書寫習(xí)慣 */ friend ostream& operator<<(ostream& output, const chongzai& A) { output << "i:" << A.i << endl; output << "j:" << A.j << endl; output << "k:" << A.k << endl; output << "------------------" << endl; return output; } friend istream& operator>>(istream& input, chongzai& A) { input >> A.i >> A.j >> A.k; return input; } /*************************************************************************************************/ /* ++和--運(yùn)算符重載: 包括前置和后置 */ chongzai operator++() //以前置++為例 { ++i; ++j; ++k; return chongzai(i, j, k); } chongzai operator++(int) //后置++的特殊格式 { i++; j++; k++; return chongzai(i, j, k); } /*************************************************************************************************/ /* 賦值運(yùn)算符重載: 賦值運(yùn)算符( = ),比如拷貝構(gòu)造函數(shù) */ void operator=(const chongzai& A) //以拷貝構(gòu)造函數(shù)為例 { i = A.i; j = A.j; k = A.k; } /*************************************************************************************************/ /* 函數(shù)調(diào)用運(yùn)算符重載: 函數(shù)調(diào)用運(yùn)算符 () 可以被重載用于類的對(duì)象。 當(dāng)重載 () 時(shí),您不是創(chuàng)造了一種新的調(diào)用函數(shù)的方式, 相反地,這是創(chuàng)建一個(gè)可以傳遞任意數(shù)目參數(shù)的運(yùn)算符函數(shù)。 */ chongzai operator()(int a, int b, int c) { chongzai A; //利用()里的參數(shù)進(jìn)行各種運(yùn)算 A.i = a + b; A.j = b + c; A.k = a + c; return A; } }; class chongzai2 { private: int arr[5]; public: int n; chongzai2() { for (n = 0; n < 5; n++) { arr[n] = n; } } /*************************************************************************************************/ /* 下標(biāo)運(yùn)算符[]重載: */ int& operator[](int n) { if (n >= 5) { cout << "索引超過(guò)最大值" << endl; return arr[0]; } return arr[n]; } };
然后這是主程序
//C++運(yùn)算符重載實(shí)例.cpp #include "標(biāo)頭.h" #include <iostream> using namespace std; int main() { chongzai A1(10, 20, 30), A2(100, 200, 300), A3; -A1; //一元運(yùn)算符重載 A1.display1(); A3 = A1 + A2; //二元運(yùn)算符重載 A3.display3(); if (A1 < A2) //關(guān)系運(yùn)算符重載 cout << "D1<D2" << endl << endl; else cout << "D1>=D2" << endl << endl; cout << "請(qǐng)輸入A3的各項(xiàng)參數(shù):" << endl; //輸入輸出元算符重載 cin >> A3; cout << "A3的各項(xiàng)參數(shù)為:" << endl; cout << A3; ++A1; //前置++運(yùn)算符重載 A1.display1(); A1++; //后置++運(yùn)算符重載 A1.display1(); A1 = A2; //賦值運(yùn)算符重載 A1.display1(); A2 = A3(2, 4, 6); //函數(shù)調(diào)用運(yùn)算符()重載 A2.display2(); chongzai2 B; cout << "B[4]的值為:" << B[4] << endl; cout << "B[5]的值為:" << B[5] << endl; //下標(biāo)運(yùn)算符[]重載 return 0; }
在輸入A3的時(shí)候,舉個(gè)例子,輸入1 2 3
輸出結(jié)果為
總結(jié)
到此這篇關(guān)于C++運(yùn)算符重載實(shí)例(調(diào)試環(huán)境 Visual Studio 2019)的文章就介紹到這了,更多相關(guān)C++運(yùn)算符重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中std::construct()與std::destroy()的使用
std::construct()和std::destroy()是C++ STL中的函數(shù)模板,用于在已分配的存儲(chǔ)區(qū)域中構(gòu)造或銷毀對(duì)象,本文主要介紹了C++中std::construct()與std::destroy()的使用,感興趣的可以了解一下2024-02-02VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境
我們?cè)趙indows10中使用VS Code做C++程序開發(fā)過(guò)程中,需要安裝MSYS2和MinGW,下面這篇文章主要給大家介紹了關(guān)于VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下2022-12-12Qt中QSettings配置文件的讀寫和應(yīng)用場(chǎng)景詳解
這篇文章主要給大家介紹了關(guān)于Qt中QSettings配置文件的讀寫和應(yīng)用場(chǎng)景的相關(guān)資料,QSettings能讀寫配置文件,當(dāng)配置文件不存在時(shí),可生成配置文件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10用C實(shí)現(xiàn)添加和讀取配置文件函數(shù)
本篇文章是對(duì)用C語(yǔ)言實(shí)現(xiàn)添加和讀取配置文件函數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++調(diào)用C#的DLL實(shí)現(xiàn)方法
這篇文章主要介紹了C++調(diào)用C#的DLL實(shí)現(xiàn)方法,很有實(shí)用價(jià)值,需要的朋友可以參考下2014-07-07C++輕量級(jí)界面開發(fā)框架ImGUI介紹小結(jié)
如果從事過(guò)C++?Windows客戶端開發(fā),大家對(duì)MFC、Qt、DuiLib等各種DirectUI應(yīng)該有了解,本篇給大家介紹一個(gè)超級(jí)輕量級(jí)的C++開源跨平臺(tái)圖形界面框架ImGUI,感興趣的可以了解一下2021-11-11c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12