C++中運算符重載詳解及其作用介紹
概述
運算符重載 (Operator Overloading)
函數(shù)重載
重載: 將同一名字重新賦予新的含義.
函數(shù)重載: 對一個函數(shù)賦予新的含義, 使之實現(xiàn)新功能. 例如:
int max(int x, int y); double max(double a, double b, double c);
運算符也有重載: 賦予運算符新的含義, 使之一名多用. 例如
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
運算符重載
通過運算符重載, 擴大了 C++ 已有運算符的作用, 使運算符能用于類對象. 使用運算符重載, 能使程序易于編寫, 閱讀和維護. 運算符被重載后, 其原有的功能仍然保留, 沒有喪失過改變.
運算符重載實質(zhì)上是函數(shù)的重載:
- 定義一個重載運算符的函數(shù)
- 需要執(zhí)行被重載的運算符時, 系統(tǒng)就自動調(diào)用該函數(shù), 以實現(xiàn)相應(yīng)的運算
C++ 的運算符
- 算數(shù)運算符: +(加) -(減) *(乘) %(整除余數(shù)) ++(自加) – (自減)
- 關(guān)系運算符: >(大于) <(小于) ==(等于) >=(大于等于) <=(小于等于) !=(不等于)
- 邏輯運算符: &&(邏輯與) ||(邏輯或) !(邏輯非)
- 位運算符: <<(按位左移) >>(按位右移) &(按位與) |(按位或) ∧(按位異或) ~(按位取反)
- 賦值運算符: = 及其擴展賦值運算符
- 條件運算符: ?:
- 都好運算符: ,
- 指針運算符: *
- 引用運算符合地址運算符: &
- 求字節(jié)數(shù)運算符: sizeof
- 強制類型轉(zhuǎn)換運算符: (類型) 或 類型()
- 成員運算符: .
- 指向成員的運算符:->
- 下標運算符: []
- 其他: 如函數(shù)調(diào)用運算符 ()
重載運算符的規(guī)則
- 不允許創(chuàng)造新的運算符, 只能對已有 C++ 運算符進行重載.
- C++ 允許重載運算符: 成員運算符(.), 成員指針訪問運算符(.*), 域運算符(:😃, 求字節(jié)數(shù)運算符(sizeof), 條件運算符(?😃
- 重載不能改變運算符運算對象 (即操作數(shù)) 的個數(shù)
- 重載不能改變運算符的優(yōu)先級別
- 重載不能改變運算符的結(jié)合性
- 重載運算符的函數(shù)不能有默認的參數(shù)
- 重載的運算符必須和用戶定義的自定義類型的對象一起使用. 參數(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)
運算符重載的方法
運算符重載格式:
函數(shù)類型 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+(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)
多種實現(xiàn)方法
成員函數(shù)實現(xiàn):
Complex Complex::operator+(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; }
簡化:
Complex Complex::operator+(Complex &c2){ return Complex(real +c2.real, imag + c2.image); }
友元函數(shù)實現(xiàn):
Complex operator+(Complex &c1, Complex &c2){ ...... }
實現(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)
三種運算符重載函數(shù)
運算符重載函數(shù)可以是類的成員函數(shù):
- 它可以通過 this 指針自由地訪問本類的數(shù)據(jù)成員. 少寫一個函數(shù)的參數(shù), 但有要求.
運算符重載函數(shù)可以是類的友元函數(shù):
- 如果運算符左側(cè)的操作屬于 C++ 標準類型 (如 int) 或是一個其他類的對象, 則運算符重載函數(shù)不能選用成員函數(shù). 為方便訪問類的私有成員, 聲明為友元函數(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ù)實現(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ù)實現(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)
重載單元運算符
單元運算符 (unary operation), 即只有一個運算量. 如: !a, -b, &c, *p, ++i, i-- 等.
例子
重載單元運算符實現(xiàn)分數(shù)對象的相反數(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); // 分數(shù)相減 Fraction operator-(); // 取反一目運算 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; // 分數(shù)相減 f4 = -f1; // 分數(shù)取反 cout << f3; cout << f4; return 0; }
輸出結(jié)果:
0.133333
-0.333333
重載二元運算符
二元運算符 (binary operation).
- 有兩個操作數(shù), 通常在運算符的左右兩側(cè) (例如: 3+2, 5>8, x*=3)
- 重載雙目運算符時, 函數(shù)中應(yīng)該有兩個參數(shù)
例子
要求:
- 定義字符串類 String, 用來存放不定長的字符串
- 重載關(guā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&,自定義類&);
插入運算符 <<
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)
提取運算符 >>
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é)
運算符重載使類的設(shè)計更加豐富多彩, 擴大了類的功能和使用范圍. 運算符重載使得程序易于理解, 易于對對象進行操作. 有了運算符重載, 在聲明了類之后, 我們就可以像使用標準類型一樣來使用自己聲明的類.
類的聲明往往是一勞永逸的. 有了好的類, 用戶在程序中就不必定義許多成員函數(shù)去完成運算和 I/O 的功能, 使主函數(shù)更加簡單易讀. 好的運算符重載能細心啊面向?qū)ο蟪绦蛟O(shè)計思想.
到此這篇關(guān)于C++中運算符重載詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++運算符重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息
NIC流量統(tǒng)計信息是由操作系統(tǒng)維護的,當數(shù)據(jù)包通過NIC傳輸時,操作系統(tǒng)會更新相關(guān)的計數(shù)器,通過讀取這些計數(shù)器,我們可以獲得關(guān)于網(wǎng)絡(luò)流量的信息,下面我們就來學習一下如何通過C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息吧2024-01-01C++中使用function和bind綁定類成員函數(shù)的方法詳解
這篇文章主要介紹了C++中使用function和bind綁定類成員函數(shù)的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表存儲詳解
鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文將和大家一起聊聊C語言中單鏈表的存儲,感興趣的可以學習一下2022-07-07