C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類詳解
首先回憶下以前學(xué)的函數(shù)重載
函數(shù)重載
- 函數(shù)重載的本質(zhì)為相互獨(dú)立的不同函數(shù)
- 通過函數(shù)名和函數(shù)參數(shù)來確定函數(shù)調(diào)用
- 無法直接通過函數(shù)名得到重載函數(shù)的入口地址
- 函數(shù)重載必然發(fā)生在同一個(gè)作用域中
類中的函數(shù)重載
- 靜態(tài)成員函數(shù)能與普通成員函數(shù)建立重載關(guān)系
- 全局函數(shù)和成員函數(shù)不能構(gòu)成重載關(guān)系
操作符重載(operator)
什么是操作符重載?
大家都知道,在C里,有'+,-,*,/'這些操作符,且它們的功能就是實(shí)現(xiàn)普通變量運(yùn)算。
由于C++是面向?qū)ο蟮?遇到的變量大多都是對(duì)象,所以優(yōu)化了C里的操作符,使它們擁有了重載能力.能通過一定方式,使對(duì)象能進(jìn)行'+,-,*,/'等運(yùn)算.
操作符的重載是以函數(shù)的方式進(jìn)行.
操作符重載定義
操作符重載,通過operator關(guān)鍵字在函數(shù)前定義:
[返回類型] operator [需要重載的操作符](函數(shù)參數(shù)) { //...... }
作符重載有幾種方式 : 全局操作符重載函數(shù)、全局操作符重載函數(shù)
編譯器首先會(huì)判斷運(yùn)算的若是對(duì)象,就會(huì)先從類里尋找成員操作符重載函數(shù),若沒找到,就會(huì)再去全局里尋找全局操作符重載函數(shù).
注意事項(xiàng):
- 操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
- 操作符重載不能改變操作數(shù)的個(gè)數(shù)
- 操作符重載的參數(shù)一般設(shè)為const class_name &類型(若只設(shè)為const class_name,會(huì)產(chǎn)生臨時(shí)對(duì)象)
- 在C++中,有些操作符必須需要有對(duì)象支持,所以只能為成員函數(shù).這種被稱為一元操作符
比如賦值(=)、下標(biāo)([])、下標(biāo)([])、調(diào)用(())和成員訪問箭頭(->):
Test t3=t2; //相當(dāng)于調(diào)用了: Test t3.operator =(t2); 里面會(huì)通過this指針來代替左側(cè)數(shù)t3
有些操作符既可以當(dāng)做成員操作符重載函數(shù),也可以當(dāng)做全局操作符重載函數(shù),由于函數(shù)參數(shù)可以多個(gè),便稱為二元操作符
比如加法(+),與(&&),或(||),逗號(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ì)通過this指針來代替左側(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.接下來,來個(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++里,沒有復(fù)數(shù)的慨念,而在剛剛又學(xué)習(xí)了操作符重載,所以接下來便通過操作符重載來實(shí)現(xiàn)復(fù)數(shù)類
復(fù)數(shù)類應(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ù)類的操作符重載共有以下幾個(gè):
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.寫測(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 //為假
以上所述是小編給大家介紹的C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類詳解詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C語言深度解剖篇之關(guān)鍵字以及補(bǔ)充內(nèi)容
C語言的關(guān)鍵字共有32個(gè),根據(jù)關(guān)鍵字的作用,可分其為數(shù)據(jù)類型關(guān)鍵字、控制語句關(guān)鍵字、存儲(chǔ)類型關(guān)鍵字和其它關(guān)鍵字四類,這篇文章主要給大家介紹了關(guān)于C語言深度解剖篇之關(guān)鍵字以及補(bǔ)充內(nèi)容的相關(guān)資料,需要的朋友可以參考下2022-06-06C++實(shí)現(xiàn)LeetCode(190.顛倒二進(jìn)制位)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(190.顛倒二進(jìn)制位),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理軟件
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05C語言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等)
這篇文章主要介紹了C語言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02