欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中運算符重載詳解及其作用介紹

 更新時間:2021年09月07日 11:08:56   作者:我是小白呀  
這篇文章主要介紹了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++ 的運算符

  1. 算數(shù)運算符: +(加) -(減) *(乘) %(整除余數(shù)) ++(自加) – (自減)
  2. 關(guān)系運算符: >(大于) <(小于) ==(等于) >=(大于等于) <=(小于等于) !=(不等于)
  3. 邏輯運算符: &&(邏輯與) ||(邏輯或) !(邏輯非)
  4. 位運算符: <<(按位左移) >>(按位右移) &(按位與) |(按位或) ∧(按位異或) ~(按位取反)
  5. 賦值運算符: = 及其擴展賦值運算符
  6. 條件運算符: ?:
  7. 都好運算符: ,
  8. 指針運算符: *
  9. 引用運算符合地址運算符: &
  10. 求字節(jié)數(shù)運算符: sizeof
  11. 強制類型轉(zhuǎn)換運算符: (類型) 或 類型()
  12. 成員運算符: .
  13. 指向成員的運算符:->
  14. 下標運算符: []
  15. 其他: 如函數(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)計信息

    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-01
  • C++標準庫介紹及使用string類的詳細過程

    C++標準庫介紹及使用string類的詳細過程

    C++中將string封裝為單獨的類,string?類是?C++?標準庫中的一個非常重要的類,用于表示和操作字符串,這篇文章主要介紹了C++標準庫介紹及使用string類,需要的朋友可以參考下
    2024-08-08
  • C++中使用function和bind綁定類成員函數(shù)的方法詳解

    C++中使用function和bind綁定類成員函數(shù)的方法詳解

    這篇文章主要介紹了C++中使用function和bind綁定類成員函數(shù)的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Matlab處理圖像后實現(xiàn)簡單的人臉檢測

    Matlab處理圖像后實現(xiàn)簡單的人臉檢測

    本文主要介紹一下如何使用matlab進行圖像處理后實現(xiàn)人臉檢測,感興趣的可以了解一下
    2021-11-11
  • C++中檢查vector是否包含給定元素的幾種方式詳解

    C++中檢查vector是否包含給定元素的幾種方式詳解

    這篇文章主要介紹了C++中檢查vector是否包含給定元素的幾種方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 詳解C++中指針和引用的區(qū)別

    詳解C++中指針和引用的區(qū)別

    這篇文章主要介紹了C++中指針和引用的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • 利用C++實現(xiàn)簡易的.ini配置文件解析器

    利用C++實現(xiàn)簡易的.ini配置文件解析器

    這篇文章主要為大家詳細介紹了如何基于C++編寫一個簡易的.ini配置文件解析器,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表存儲詳解

    C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表存儲詳解

    鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文將和大家一起聊聊C語言中單鏈表的存儲,感興趣的可以學習一下
    2022-07-07
  • 詳解c++中的異常

    詳解c++中的異常

    程序在運行過程中,有對也就有錯,正確那么就不用說了,但是如果錯誤,那么我們?nèi)绾慰焖俚亩ㄎ坏藉e誤的位置,以及知道發(fā)生了什么錯誤。當一個函數(shù)發(fā)現(xiàn)自己無法處理的異常,就會拋出一個異常,讓函數(shù)調(diào)用者直接或者間接的處理這個錯誤。本文將詳解介紹c++中的異常
    2021-06-06
  • C語言實現(xiàn)大學生考勤管理系統(tǒng)

    C語言實現(xiàn)大學生考勤管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)大學生考勤管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評論