国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++-操作符重載、并實現復數類詳解

2020-01-26 13:30:32
字體:
來源:轉載
供稿:網友

首先回憶下以前學的函數重載

函數重載

  1. 函數重載的本質為相互獨立的不同函數
  2. 通過函數名和函數參數來確定函數調用
  3. 無法直接通過函數名得到重載函數的入口地址
  4. 函數重載必然發生在同一個作用域中

類中的函數重載

  1. 靜態成員函數能與普通成員函數建立重載關系
  2. 全局函數和成員函數不能構成重載關系

操作符重載(operator)

什么是操作符重載?

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

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

操作符的重載是以函數的方式進行.

操作符重載定義

操作符重載,通過operator關鍵字在函數前定義:

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

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

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

注意事項:

  1. 操作符重載不能改變原操作符的優先級
  2. 操作符重載不能改變操作數的個數
  3. 操作符重載的參數一般設為const class_name &類型(若只設為const class_name,會產生臨時對象)
  4. 在C++中,有些操作符必須需要有對象支持,所以只能為成員函數.這種被稱為一元操作符

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

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++-操作符重載、并實現復數類詳解詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 定南县| 抚远县| 方山县| 合水县| 深圳市| 长阳| 泊头市| 深水埗区| 晋中市| 修文县| 公主岭市| 秦皇岛市| 美姑县| 竹山县| 桃园县| 北川| 岫岩| 湖州市| 林口县| 中方县| 娱乐| 嘉荫县| 松江区| 玉龙| 中阳县| 大洼县| 平武县| 永安市| 忻城县| 鹿泉市| 磴口县| 黑水县| 淮滨县| 东安县| 怀柔区| 固安县| 西林县| 长汀县| 兴仁县| 巴彦淖尔市|