C++中運(yùn)算符重載詳解及其作用介紹
概述
運(yùn)算符重載 (Operator Overloading)
函數(shù)重載
重載: 將同一名字重新賦予新的含義.
函數(shù)重載: 對(duì)一個(gè)函數(shù)賦予新的含義, 使之實(shí)現(xiàn)新功能. 例如:
int max(int x, int y); double max(double a, double b, double c);
運(yùn)算符也有重載: 賦予運(yùn)算符新的含義, 使之一名多用. 例如
int main() { int i = 2, j = 3; int k = i + j; string s1 = "good ", s2 = "morning"; string s3 = s1 + s2; cout << k << endl; cout << s3 << endl; return 0; }
輸出結(jié)果:
5
good morning
運(yùn)算符重載
通過運(yùn)算符重載, 擴(kuò)大了 C++ 已有運(yùn)算符的作用, 使運(yùn)算符能用于類對(duì)象. 使用運(yùn)算符重載, 能使程序易于編寫, 閱讀和維護(hù). 運(yùn)算符被重載后, 其原有的功能仍然保留, 沒有喪失過改變.
運(yùn)算符重載實(shí)質(zhì)上是函數(shù)的重載:
- 定義一個(gè)重載運(yùn)算符的函數(shù)
- 需要執(zhí)行被重載的運(yùn)算符時(shí), 系統(tǒng)就自動(dòng)調(diào)用該函數(shù), 以實(shí)現(xiàn)相應(yīng)的運(yùn)算
C++ 的運(yùn)算符
- 算數(shù)運(yùn)算符: +(加) -(減) *(乘) %(整除余數(shù)) ++(自加) – (自減)
- 關(guān)系運(yùn)算符: >(大于) <(小于) ==(等于) >=(大于等于) <=(小于等于) !=(不等于)
- 邏輯運(yùn)算符: &&(邏輯與) ||(邏輯或) !(邏輯非)
- 位運(yùn)算符: <<(按位左移) >>(按位右移) &(按位與) |(按位或) ∧(按位異或) ~(按位取反)
- 賦值運(yùn)算符: = 及其擴(kuò)展賦值運(yùn)算符
- 條件運(yùn)算符: ?:
- 都好運(yùn)算符: ,
- 指針運(yùn)算符: *
- 引用運(yùn)算符合地址運(yùn)算符: &
- 求字節(jié)數(shù)運(yùn)算符: sizeof
- 強(qiáng)制類型轉(zhuǎn)換運(yùn)算符: (類型) 或 類型()
- 成員運(yùn)算符: .
- 指向成員的運(yùn)算符:->
- 下標(biāo)運(yùn)算符: []
- 其他: 如函數(shù)調(diào)用運(yùn)算符 ()
重載運(yùn)算符的規(guī)則
- 不允許創(chuàng)造新的運(yùn)算符, 只能對(duì)已有 C++ 運(yùn)算符進(jìn)行重載.
- C++ 允許重載運(yùn)算符: 成員運(yùn)算符(.), 成員指針訪問運(yùn)算符(.*), 域運(yùn)算符(:😃, 求字節(jié)數(shù)運(yùn)算符(sizeof), 條件運(yùn)算符(?😃
- 重載不能改變運(yùn)算符運(yùn)算對(duì)象 (即操作數(shù)) 的個(gè)數(shù)
- 重載不能改變運(yùn)算符的優(yōu)先級(jí)別
- 重載不能改變運(yùn)算符的結(jié)合性
- 重載運(yùn)算符的函數(shù)不能有默認(rèn)的參數(shù)
- 重載的運(yùn)算符必須和用戶定義的自定義類型的對(duì)象一起使用. 參數(shù)至少有一個(gè)是類對(duì)象或其 引用
成員函數(shù)實(shí)現(xiàn) Complex 加法
Complex 類:
#ifndef PROJECT2_COMPLEX_H #define PROJECT2_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double r, double i); Complex add(Complex &c2); void display(); }; #endif //PROJECT2_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) : real(r), imag(i) {} Complex Complex::add(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; } void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; }
main:
int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1.add(c2); cout << "c1 + c2 = "; c3.display(); return 0; }
輸出結(jié)果:
c1 =(3, 4i)
c2 =(5, -10i)
c1 + c2 = (8, -6i)
運(yùn)算符重載的方法
運(yùn)算符重載格式:
函數(shù)類型 operator 運(yùn)算符名稱 (形參流標(biāo)) {對(duì)運(yùn)算符的重載處理}
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c2); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1 + c2; cout << "c1 + c2 = "; c3.display(); return 0; }
輸出結(jié)果:
c1 =(3, 4i)
c2 =(5, -10i)
c3= (8, -6i)
多種實(shí)現(xiàn)方法
成員函數(shù)實(shí)現(xiàn):
Complex Complex::operator+(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; }
簡(jiǎn)化:
Complex Complex::operator+(Complex &c2){ return Complex(real +c2.real, imag + c2.image); }
友元函數(shù)實(shí)現(xiàn):
Complex operator+(Complex &c1, Complex &c2){ ...... }
實(shí)現(xiàn) operator+=
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+=(const Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+=(const Complex &c) { real += c.real; // this->real += c.real; imag += c.imag; // this->imag += c.imag; return *this; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c1 += c2; cout << "c1= "; c1.display(); return 0; }
輸出結(jié)果:
c1 =(3, 4i)
c2 =(5, -10i)
c1= (8, -6i)
三種運(yùn)算符重載函數(shù)
運(yùn)算符重載函數(shù)可以是類的成員函數(shù):
- 它可以通過 this 指針自由地訪問本類的數(shù)據(jù)成員. 少寫一個(gè)函數(shù)的參數(shù), 但有要求.
運(yùn)算符重載函數(shù)可以是類的友元函數(shù):
- 如果運(yùn)算符左側(cè)的操作屬于 C++ 標(biāo)準(zhǔn)類型 (如 int) 或是一個(gè)其他類的對(duì)象, 則運(yùn)算符重載函數(shù)不能選用成員函數(shù). 為方便訪問類的私有成員, 聲明為友元函數(shù)為佳.
運(yùn)算符重載函數(shù)還可以是普通函數(shù):
- 只有極少的情況下才使用 (因普通函數(shù)一般不能直接訪問類的私有成員)
成員函數(shù)實(shí)現(xiàn)
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(double d); // 成員函數(shù)實(shí)現(xiàn) }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(double d) { return Complex(real + d, imag); }
友元函數(shù)實(shí)現(xiàn)
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); friend Complex operator+(Complex &c, double d); // 友元函數(shù) }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex operator+(Complex &c, double d) { return Complex(c.real + d, c.imag); }
輸出結(jié)果
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3, c4; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1 + 3.14; cout << "c3= "; c3.display(); return 0; }
輸出結(jié)果:
c1 =(3, 4i)
c2 =(5, -10i)
c3= (6.14, 4i)
重載單元運(yùn)算符
單元運(yùn)算符 (unary operation), 即只有一個(gè)運(yùn)算量. 如: !a, -b, &c, *p, ++i, i-- 等.
例子
重載單元運(yùn)算符實(shí)現(xiàn)分?jǐn)?shù)對(duì)象的相反數(shù).
Fraction 類:
#ifndef PROJECT4_FRACTION_H #define PROJECT4_FRACTION_H #include <iostream> using namespace std; class Fraction { private: int nume; // 分子 int deno; // 分母 public: Fraction(); Fraction(int, int); Fraction operator-(const Fraction &c); // 分?jǐn)?shù)相減 Fraction operator-(); // 取反一目運(yùn)算 friend ostream& operator<<(ostream &output, const Fraction &f); }; #endif //PROJECT4_FRACTION_H
Fraction.cpp:
#include "Fraction.h" Fraction::Fraction() : nume(0), deno(0) {} Fraction::Fraction(int n , int d) : nume(n), deno(d) {} Fraction Fraction::operator-(const Fraction &c) { return Fraction(nume*c.deno - c.nume*deno, deno*c.deno); } Fraction Fraction::operator-() { return Fraction(-nume, deno); } ostream& operator<<(ostream &output, const Fraction &f) { double result = (double)f.nume / f.deno; output << result << endl; return output; }
main:
#include <iostream> #include "Fraction.h" using namespace std; int main() { Fraction f1(1,3), f2(1,5), f3, f4; f3 = f1 - f2; // 分?jǐn)?shù)相減 f4 = -f1; // 分?jǐn)?shù)取反 cout << f3; cout << f4; return 0; }
輸出結(jié)果:
0.133333
-0.333333
重載二元運(yùn)算符
二元運(yùn)算符 (binary operation).
- 有兩個(gè)操作數(shù), 通常在運(yùn)算符的左右兩側(cè) (例如: 3+2, 5>8, x*=3)
- 重載雙目運(yùn)算符時(shí), 函數(shù)中應(yīng)該有兩個(gè)參數(shù)
例子
要求:
- 定義字符串類 String, 用來存放不定長(zhǎng)的字符串
- 重載關(guān)系運(yùn)算符, 用于兩個(gè)字符串的比較運(yùn)算
步驟:
- 定義類的 “框架”
- 完善運(yùn)算符重載
String 類:
#ifndef PROJECT4_STRING_H #define PROJECT4_STRING_H #include <cstdlib> class String { private: char *p; public: String(){p=nullptr;} String(char *str); void display(); }; #endif //PROJECT4_STRING_H
String.cpp:
#include <iostream> #include <cstring> #include "String.h" using namespace std; String::String(char *str) { p = new char[sizeof(str)]; strcpy(p, str); } void String::display() { cout << p; }
main:
#include <iostream> #include "String.h" using namespace std; int main() { String s1("Hello"); String s2("China"); s1.display( ); cout<<" "; s2.display( ); cout<<endl; return 0; }
輸出結(jié)果:
Hello China
重載 I/O
通過重載輸入流 (input stream) 和輸出流 (output stream), 我們可以用來輸出用戶自己定義的數(shù)據(jù).
格式:
ostream &operator<<(ostream&, const 自定義類&); istream &operator>>(istream&,自定義類&);
插入運(yùn)算符 <<
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c); friend ostream& operator<<(ostream &output, const Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c) { return Complex(real + c.real, imag + c.imag); } ostream &operator<<(ostream &output, const Complex &c) { output<<"("<<c.real<<" + "<<c.imag<<"i)"; return output; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(2, 4),c2(6, 10),c3; c3 = c1 + c2; cout << c1 << " + " << c2 << " = " << c3 << endl; return 0; }
輸出結(jié)果:
(2 + 4i) + (6 + 10i) = (8 + 14i)
提取運(yùn)算符 >>
Complex 類:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c); friend ostream& operator<<(ostream &output, const Complex &c); friend istream& operator>>(istream &input, Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c) { return Complex(real + c.real, imag + c.imag); } ostream &operator<<(ostream &output, const Complex &c) { output<<"("<<c.real<<" + "<<c.imag<<"i)"; return output; } istream &operator>>(istream &input, Complex &c) { cout << "input real part and imaginary part:\n"; input >> c.real >> c.imag; return input; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1, c2; cin >> c1 >> c2; cout << "c1=" << c1 << endl; cout << "c2=" << c2 << endl; return 0; }
輸出結(jié)果:
input real part and imaginary part:
2 4
input real part and imaginary part:
6 10
c1=(2 + 4i)
c2=(6 + 10i)
總結(jié)
運(yùn)算符重載使類的設(shè)計(jì)更加豐富多彩, 擴(kuò)大了類的功能和使用范圍. 運(yùn)算符重載使得程序易于理解, 易于對(duì)對(duì)象進(jìn)行操作. 有了運(yùn)算符重載, 在聲明了類之后, 我們就可以像使用標(biāo)準(zhǔn)類型一樣來使用自己聲明的類.
類的聲明往往是一勞永逸的. 有了好的類, 用戶在程序中就不必定義許多成員函數(shù)去完成運(yùn)算和 I/O 的功能, 使主函數(shù)更加簡(jiǎn)單易讀. 好的運(yùn)算符重載能細(xì)心啊面向?qū)ο蟪绦蛟O(shè)計(jì)思想.
到此這篇關(guān)于C++中運(yùn)算符重載詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++運(yùn)算符重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言異或校驗(yàn)算法的項(xiàng)目實(shí)現(xiàn)
異或校驗(yàn)算法(XOR校驗(yàn))是一種簡(jiǎn)單的校驗(yàn)算法,用于檢測(cè)數(shù)據(jù)在傳輸或存儲(chǔ)過程中是否發(fā)生了錯(cuò)誤,本文主要介紹了C語言異或校驗(yàn)算法的項(xiàng)目實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08c++動(dòng)態(tài)內(nèi)存空間示例(自定義空間類型大小和空間長(zhǎng)度)
這篇文章主要介紹了c++動(dòng)態(tài)內(nèi)存空間示例,自定義空間類型大小和空間長(zhǎng)度,需要的朋友可以參考下2014-04-04C語言實(shí)現(xiàn)簡(jiǎn)單的文本編輯器
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單的文本編輯器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05C++ float轉(zhuǎn)std::string 小數(shù)位數(shù)控制問題
這篇文章主要介紹了C++ float轉(zhuǎn)std::string 小數(shù)位數(shù)控制問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11