
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策制定
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
C++ 過載(運算子和函式)
C++ 允許您在同一作用域中為函式名稱或運算子指定多個定義,分別稱為函式過載和運算子過載。
過載宣告是在同一作用域中宣告與先前宣告的宣告具有相同名稱的宣告,但這兩個宣告具有不同的引數,並且顯然具有不同的定義(實現)。
當您呼叫過載函式或運算子時,編譯器會透過將您用來呼叫函式或運算子的引數型別與定義中指定的引數型別進行比較,來確定最合適的定義。選擇最合適的過載函式或運算子的過程稱為過載解析。
C++ 中的函式過載
您可以在同一作用域中為同一個函式名稱定義多個定義。函式的定義必須透過引數列表中引數的型別和/或數量相互區別。您不能過載僅返回值型別不同的函式宣告。
閱讀: C++ 函式過載
以下是使用相同的函式print()列印不同資料型別的示例:
#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; }
當以上程式碼編譯並執行時,會產生以下結果:
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
C++ 中的運算子過載
您可以重新定義或過載 C++ 中可用的大多數內建運算子。因此,程式設計師也可以將運算子與使用者定義的型別一起使用。
過載運算子是具有特殊名稱的函式:“operator”關鍵字後跟要定義的運算子的符號。像任何其他函式一樣,過載運算子具有返回型別和引數列表。
Box operator+(const Box&);
聲明瞭可以用來新增兩個 Box 物件並返回最終 Box 物件的加法運算子。大多數過載運算子可以定義為普通的非成員函式或類成員函式。如果我們將上述函式定義為類的非成員函式,則必須為每個運算元傳遞兩個引數,如下所示:
Box operator+(const Box&, const Box&);
以下示例演示了使用成員函式的概念來進行運算子過載。這裡傳遞一個作為引數的物件,其屬性將使用此物件進行訪問,呼叫此運算子的物件可以使用this運算子進行訪問,如下所述:
#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload + operator to add two Box objects. Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Main function for the program int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // volume of box 1 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // Add two object as follows: Box3 = Box1 + Box2; // volume of box 3 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }
當以上程式碼編譯並執行時,會產生以下結果:
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
可過載/不可過載運算子
以下是可過載的運算子列表:
+ | - | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | -- |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [] | () |
-> | ->* | new | new [] | delete | delete [] |
以下是不可過載的運算子列表:
:: | .* | . | ?: |
運算子過載示例
以下是一些運算子過載示例,以幫助您理解該概念。
序號 | 運算子和示例 |
---|---|
1 | 一元運算子過載 |
2 | 二元運算子過載 |
3 | 關係運算符過載 |
4 | 輸入/輸出運算子過載 |
5 | ++ 和 -- 運算子過載 |
6 | 賦值運算子過載 |
7 | 函式呼叫 () 運算子過載 |
8 | 下標 [] 運算子過載 |
9 | 類成員訪問運算子 -> 過載 |
廣告