首先回憶下以前學的函數重載
函數重載
類中的函數重載
操作符重載(operator)
什么是操作符重載?
大家都知道,在C里,有'+,-,*,/'這些操作符,且它們的功能就是實現普通變量運算。
由于C++是面向對象的,遇到的變量大多都是對象,所以優化了C里的操作符,使它們擁有了重載能力.能通過一定方式,使對象能進行'+,-,*,/'等運算.
操作符的重載是以函數的方式進行.
操作符重載定義
操作符重載,通過operator關鍵字在函數前定義:
[返回類型] operator [需要重載的操作符](函數參數) { //...... }作符重載有幾種方式 : 全局操作符重載函數、全局操作符重載函數
編譯器首先會判斷運算的若是對象,就會先從類里尋找成員操作符重載函數,若沒找到,就會再去全局里尋找全局操作符重載函數.
注意事項:
比如賦值(=)、下標([])、下標([])、調用(())和成員訪問箭頭(->):
Test t3=t2; //相當于調用了: Test t3.operator =(t2); 里面會通過this指針來代替左側數t3
有些操作符既可以當做成員操作符重載函數,也可以當做全局操作符重載函數,由于函數參數可以多個,便稱為二元操作符
比如加法(+),與(&&),或(||),逗號(,)等:
以加法(+)為例,當設為全局操作符重載函數時,執行
Test t3=t1+t2; //相當于調用了: Test t3 = operator +(t1,t2);
以加法(+)為例,當設為成員操作符重載函數時,執行
Test t3=t1+t2; //相當于調用了: Test t3 =t1.operator +(t2); //里面會通過this指針來代替左側數t1
多個重載的操作符重載函數
由于操作符重載函數帶參數,所以可以存在多個相同的操作符重載函數
例如:
class Test{ double x; double y; public: Test operator +(const Test& t); //實現Test t3=t1+t2 Test operator +(int i); //實現Test t3=t1+1 Test operator +(double d); //實現Test t3=t1+1.25 //... ... };初步試驗
1.接下來,來個全局操作符重載函數例子:
#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); //聲明友元函數,可以使用私有成員變量 }; 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; // 其實就是調用: 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;}打印結果:
t3.x:3 t3.y:7
t4.x:4 t4.y:10
換成成員操作符重載函數例子:
#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; // 其實就是調用: 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; }打印結果:
t3.x:3 t3.y:7
t4.x:4 t4.y:10
加深理解
由于C++里,沒有復數的慨念,而在剛剛又學習了操作符重載,所以接下來便通過操作符重載來實現復數類
復數類應該具有
兩個成員
實部a 、虛部b
運算操作符
+ - : 結果 = 兩個實部進行加減,兩個虛部進行加減
* : 結果 = (a1+b1)(a2+b2)= (a1*a2 - b1*b2 )+( a2*b1 + a1*b2);
/ : 結果 =(a1+b1)/(a2+b2)= (a1*a2+b1*b2)/(a2* a2+b2* b2) +(b1*a2-a1*b2)/(a2* a2+b2* b2)
比較操作符:== ,!=
賦值操作符: =
求模成員函數 : 等于a^2+b^2的算術平方根
所以復數類的操作符重載共有以下幾個:

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 //為假
以上所述是小編給大家介紹的C++-操作符重載、并實現復數類詳解詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答
圖片精選