怎么在C++中重載操作符

這篇文章給大家介紹怎么在C++中重載操作符,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比北票網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式北票網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋北票地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

函數(shù)重載

  1. 函數(shù)重載的本質(zhì)為相互獨立的不同函數(shù)

  2. 通過函數(shù)名和函數(shù)參數(shù)來確定函數(shù)調(diào)用

  3. 無法直接通過函數(shù)名得到重載函數(shù)的入口地址

  4. 函數(shù)重載必然發(fā)生在同一個作用域中

類中的函數(shù)重載

  1. 靜態(tài)成員函數(shù)能與普通成員函數(shù)建立重載關(guān)系

  2. 全局函數(shù)和成員函數(shù)不能構(gòu)成重載關(guān)系

操作符重載(operator)

什么是操作符重載?

大家都知道,在C里,有'+,-,*,/'這些操作符,且它們的功能就是實現(xiàn)普通變量運算。

由于C++是面向?qū)ο蟮?遇到的變量大多都是對象,所以優(yōu)化了C里的操作符,使它們擁有了重載能力.能通過一定方式,使對象能進(jìn)行'+,-,*,/'等運算.

操作符的重載是以函數(shù)的方式進(jìn)行.

操作符重載定義

操作符重載,通過operator關(guān)鍵字在函數(shù)前定義:

[返回類型] operator [需要重載的操作符](函數(shù)參數(shù))
 
{
 
    //......
 
}

作符重載有幾種方式 : 全局操作符重載函數(shù)、全局操作符重載函數(shù)

編譯器首先會判斷運算的若是對象,就會先從類里尋找成員操作符重載函數(shù),若沒找到,就會再去全局里尋找全局操作符重載函數(shù).

注意事項:

  1. 操作符重載不能改變原操作符的優(yōu)先級

  2. 操作符重載不能改變操作數(shù)的個數(shù)

  3. 操作符重載的參數(shù)一般設(shè)為const class_name &類型(若只設(shè)為const class_name,會產(chǎn)生臨時對象)

  4. 在C++中,有些操作符必須需要有對象支持,所以只能為成員函數(shù).這種被稱為一元操作符

比如賦值(=)、下標(biāo)([])、下標(biāo)([])、調(diào)用(())和成員訪問箭頭(->):

Test t3=t2;  //相當(dāng)于調(diào)用了: Test t3.operator =(t2); 里面會通過this指針來代替左側(cè)數(shù)t3

有些操作符既可以當(dāng)做成員操作符重載函數(shù),也可以當(dāng)做全局操作符重載函數(shù),由于函數(shù)參數(shù)可以多個,便稱為二元操作符
比如加法(+),與(&&),或(||),逗號(,)等:

以加法(+)為例,當(dāng)設(shè)為全局操作符重載函數(shù)時,執(zhí)行

Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 = operator +(t1,t2);

以加法(+)為例,當(dāng)設(shè)為成員操作符重載函數(shù)時,執(zhí)行

Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 =t1.operator +(t2); //里面會通過this指針來代替左側(cè)數(shù)t1

多個重載的操作符重載函數(shù)

由于操作符重載函數(shù)帶參數(shù),所以可以存在多個相同的操作符重載函數(shù)

例如:

class Test
{
 
double x;
 
double y;
 
public:
 
Test operator +(const Test& t);        //實現(xiàn)Test t3=t1+t2
 
Test operator +(int i);            //實現(xiàn)Test t3=t1+1
 
Test operator +(double d);           //實現(xiàn)Test t3=t1+1.25
 
//... ...
 
};

初步試驗

1.接下來,來個全局操作符重載函數(shù)例子:

#include "stdio.h"
 
 
class Test{
 
    int x;
    int y;
 
public:
    Test(int x=0,int y=0)
    {
       this->x=x;
       this->y=y;  
    }
    int getx()
    {
       return x;
    }
    int gety()
    {
       return y;
    }
 
    friend Test operator + (const Test& t1,const Test& t2);
                         //聲明友元函數(shù),可以使用私有成員變量  
 
};
 
Test operator + (const Test& t1,const Test& t2)       //重載
{
    Test ret;
    ret.x=t1.x+t2.x;
    ret.y=t1.y+t2.y;
    return ret;
}
 
int main()
{
    Test t1(1,3);
    Test t2(2,4);
    Test t3= t1 + t2;      // 其實就是調(diào)用: Test t3 = operator +(t1,t2);
 
    printf("t3.x:%d t3.y:%d\n",t3.getx(),t3.gety()); 
 
    Test t4 =operator +(t1,t3);  // t4 =t1 +t3
 
    printf("t4.x:%d t4.y:%d\n",t4.getx(),t4.gety());
 
    return 0;
}

打印結(jié)果:

t3.x:3  t3.y:7
t4.x:4  t4.y:10

 換成成員操作符重載函數(shù)例子:

#include "stdio.h"
 
class Test{
 
    int x;
    int y;
 
public:
    Test(int x=0,int y=0)
    {
     this->x =x;
     this->y =y;
    }
 
    int getx()
    {
       return x;
    }
 
    int gety()
    {
       return y;
    } 
 
  Test operator + (const Test& t2)
    {
       Test ret;
 
       ret.x = this->x + t2.x;
       ret.y = this->y + t2.y;
       return ret;
    }                
};
 
int main()
{
    Test t1(1,3);
    Test t2(2,4);
    Test t3= t1 + t2;      // 其實就是調(diào)用: Test t3 =t1.operator +(t2);
 
    printf("t3.x:%d t3.y:%d\n",t3.getx(),t3.gety()); 
 
    Test t4 =t1.operator +(t3);  // t4 =t1 +t3
 
    printf("t4.x:%d t4.y:%d\n",t4.getx(),t4.gety());
 
    return 0;
    }

打印結(jié)果:

t3.x:3  t3.y:7
t4.x:4  t4.y:10

 加深理解

由于C++里,沒有復(fù)數(shù)的慨念,而在剛剛又學(xué)習(xí)了操作符重載,所以接下來便通過操作符重載來實現(xiàn)復(fù)數(shù)類

復(fù)數(shù)類應(yīng)該具有

兩個成員

實部a 、虛部b

運算操作符

+ - :  結(jié)果 = 兩個實部進(jìn)行加減,兩個虛部進(jìn)行加減

*   :  結(jié)果 = (a1+b1)(a2+b2)= (a1*a2 - b1*b2 )+( a2*b1 + a1*b2);

/   :  結(jié)果 =(a1+b1)/(a2+b2)= (a1*a2+b1*b2)/(a2* a2+b2* b2) +(b1*a2-a1*b2)/(a2* a2+b2* b2)

比較操作符:== ,!=

賦值操作符: =

求模成員函數(shù) : 等于a^2+b^2的算術(shù)平方根

所以復(fù)數(shù)類的操作符重載共有以下幾個:

怎么在C++中重載操作符

1.寫頭文件Complex.h:

#ifndef __COMPLEX_H
#define __COMPLEX_H
 
class Complex{
 
private:
    double a;
    double b;
 
public:
    Complex(int a=0,int b=0);
    Complex operator + (const Complex& t);
    Complex operator - (const Complex& t);
    Complex operator * (const Complex& t);
    Complex operator / (const Complex& t);
    bool operator == (const Complex& t);
    bool operator != (const Complex& t);
    Complex& operator = (const Complex& t);
 
    double getModulus();
 
    double getA();
    double getB();
};
 
#endif

2.寫源文件Complex.cpp

#include "Complex.h"
#include "math.h"
 
Complex::Complex(int a,int b)
{
    this->a = a;
    this->b = b;
}
 
Complex Complex::operator + (const Complex& t)
{
    Complex ret; 
 
    ret.a = a + t.a;
    ret.b = b + t.b;
    return ret;
} 
 
Complex Complex::operator - (const Complex& t)
{
    Complex ret; 
 
    ret.a = a - t.a;
    ret.b = b - t.b;
    return ret;
}
 
    
 
Complex Complex::operator * (const Complex& t)
{
    Complex ret;
    ret.a = (a* t.a - b* t.b );
    ret.b = (t.a *b + a* t.b );  
    return ret;
}
 
 
 
    
 
Complex Complex::operator / (const Complex& t)
{
    Complex ret;
    ret.a = (a* t.a + b* t.b)/(t.a * t.a + t.b * t.b);
    ret.b = (b* t.a - a* t.b)/(t.a * t.a + t.b * t.b);  
    return ret;
}
 
    
 
bool Complex::operator == (const Complex& t)
{
    if((a== t.a)&&(b== t.b))
    return true;
 
    else
    return false;
}  
 
bool Complex::operator != (const Complex& t)
{
    if((a!= t.a)||(b!= t.b))
    return true;
 
    else
    return false;
}
 
Complex& Complex::operator = (const Complex& t)
{
    if(this != &t)
    {
     a = t.a;
     b = t.b;
    }
    return *this;
}  
 
double Complex::getModulus()
{
    return sqrt( a*a + b*b);
}
   
 
double Complex::getA()
{
    return a;
}  
 
double Complex::getB()
{
    return b;
}

3.寫測試文件test.cpp

#include "stdio.h"
#include "Complex.h"
 
int main()
{
    Complex t1(1,3);
    Complex t2(2,6);
 
    Complex t3=t1+t2;
 
    printf("t3.a=%f t3.b=%f\n",t3.getA(),t3.getB());
 
    printf("t3 Modulus:%f\n",t3.getModulus());
 
    Complex t4=t3;
 
    printf("t4==t3: %d\n",t4==t3);
    printf("t4!=t3: %d\n",t4!=t3);
    printf("t3==t1: %d\n",t3==t1);
 
    return 0;
}

4.編譯運行

t3.a=3.000000  t3.b=9.000000
t3 Modulus:9.486833
t4==t3: 1                                   //為真
t4!=t3: 0                                   //為假
t3==t1: 0                                   //為假

關(guān)于怎么在C++中重載操作符就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:怎么在C++中重載操作符
本文網(wǎng)址:http://muchs.cn/article14/phosge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、虛擬主機、品牌網(wǎng)站設(shè)計網(wǎng)站維護、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)