這篇文章給大家介紹怎么在C++中重載操作符,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比北票網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式北票網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋北票地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴(lài)。
函數(shù)重載
函數(shù)重載的本質(zhì)為相互獨(dú)立的不同函數(shù)
通過(guò)函數(shù)名和函數(shù)參數(shù)來(lái)確定函數(shù)調(diào)用
無(wú)法直接通過(guò)函數(shù)名得到重載函數(shù)的入口地址
函數(shù)重載必然發(fā)生在同一個(gè)作用域中
類(lèi)中的函數(shù)重載
靜態(tài)成員函數(shù)能與普通成員函數(shù)建立重載關(guān)系
全局函數(shù)和成員函數(shù)不能構(gòu)成重載關(guān)系
操作符重載(operator)
什么是操作符重載?
大家都知道,在C里,有'+,-,*,/'這些操作符,且它們的功能就是實(shí)現(xiàn)普通變量運(yùn)算。
由于C++是面向?qū)ο蟮?遇到的變量大多都是對(duì)象,所以?xún)?yōu)化了C里的操作符,使它們擁有了重載能力.能通過(guò)一定方式,使對(duì)象能進(jìn)行'+,-,*,/'等運(yùn)算.
操作符的重載是以函數(shù)的方式進(jìn)行.
操作符重載定義
操作符重載,通過(guò)operator關(guān)鍵字在函數(shù)前定義:
[返回類(lèi)型] operator [需要重載的操作符](函數(shù)參數(shù))
{
//......
}作符重載有幾種方式 : 全局操作符重載函數(shù)、全局操作符重載函數(shù)
編譯器首先會(huì)判斷運(yùn)算的若是對(duì)象,就會(huì)先從類(lèi)里尋找成員操作符重載函數(shù),若沒(méi)找到,就會(huì)再去全局里尋找全局操作符重載函數(shù).
注意事項(xiàng):
操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
操作符重載不能改變操作數(shù)的個(gè)數(shù)
操作符重載的參數(shù)一般設(shè)為const class_name &類(lèi)型(若只設(shè)為const class_name,會(huì)產(chǎn)生臨時(shí)對(duì)象)
在C++中,有些操作符必須需要有對(duì)象支持,所以只能為成員函數(shù).這種被稱(chēng)為一元操作符
比如賦值(=)、下標(biāo)([])、下標(biāo)([])、調(diào)用(())和成員訪問(wèn)箭頭(->):
Test t3=t2; //相當(dāng)于調(diào)用了: Test t3.operator =(t2); 里面會(huì)通過(guò)this指針來(lái)代替左側(cè)數(shù)t3
有些操作符既可以當(dāng)做成員操作符重載函數(shù),也可以當(dāng)做全局操作符重載函數(shù),由于函數(shù)參數(shù)可以多個(gè),便稱(chēng)為二元操作符
比如加法(+),與(&&),或(||),逗號(hào)(,)等:
以加法(+)為例,當(dāng)設(shè)為全局操作符重載函數(shù)時(shí),執(zhí)行
Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 = operator +(t1,t2);
以加法(+)為例,當(dāng)設(shè)為成員操作符重載函數(shù)時(shí),執(zhí)行
Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 =t1.operator +(t2); //里面會(huì)通過(guò)this指針來(lái)代替左側(cè)數(shù)t1
多個(gè)重載的操作符重載函數(shù)
由于操作符重載函數(shù)帶參數(shù),所以可以存在多個(gè)相同的操作符重載函數(shù)
例如:
class Test
{
double x;
double y;
public:
Test operator +(const Test& t); //實(shí)現(xiàn)Test t3=t1+t2
Test operator +(int i); //實(shí)現(xiàn)Test t3=t1+1
Test operator +(double d); //實(shí)現(xiàn)Test t3=t1+1.25
//... ...
};初步試驗(yàn)
1.接下來(lái),來(lái)個(gè)全局操作符重載函數(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; // 其實(shí)就是調(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; // 其實(shí)就是調(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++里,沒(méi)有復(fù)數(shù)的慨念,而在剛剛又學(xué)習(xí)了操作符重載,所以接下來(lái)便通過(guò)操作符重載來(lái)實(shí)現(xiàn)復(fù)數(shù)類(lèi)
復(fù)數(shù)類(lèi)應(yīng)該具有
兩個(gè)成員
實(shí)部a 、虛部b
運(yùn)算操作符
+ - : 結(jié)果 = 兩個(gè)實(shí)部進(jìn)行加減,兩個(gè)虛部進(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ù)類(lèi)的操作符重載共有以下幾個(gè):

1.寫(xiě)頭文件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();
};
#endif2.寫(xiě)源文件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.寫(xiě)測(cè)試文件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.編譯運(yùn)行
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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享文章:怎么在C++中重載操作符
本文網(wǎng)址:http://chinadenli.net/article14/phosge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、虛擬主機(jī)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)