欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類(lèi)詳解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

首先回憶下以前學(xué)的函數(shù)重載

函數(shù)重載

  1. 函數(shù)重載的本質(zhì)為相互獨(dú)立的不同函數(shù)
  2. 通過(guò)函數(shù)名和函數(shù)參數(shù)來(lái)確定函數(shù)調(diào)用
  3. 無(wú)法直接通過(guò)函數(shù)名得到重載函數(shù)的入口地址
  4. 函數(shù)重載必然發(fā)生在同一個(gè)作用域中

類(lèi)中的函數(shù)重載

  1. 靜態(tài)成員函數(shù)能與普通成員函數(shù)建立重載關(guān)系
  2. 全局函數(shù)和成員函數(shù)不能構(gòu)成重載關(guān)系

操作符重載(operator)

什么是操作符重載?

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

由于C++是面向?qū)ο蟮?遇到的變量大多都是對(duì)象,所以優(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):

  1. 操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
  2. 操作符重載不能改變操作數(shù)的個(gè)數(shù)
  3. 操作符重載的參數(shù)一般設(shè)為const class_name &類(lèi)型(若只設(shè)為const class_name,會(huì)產(chǎn)生臨時(shí)對(duì)象)
  4. 在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();
};
 
#endif
 

2.寫(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                                   //為假

以上所述是小編給大家介紹的C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類(lèi)詳解詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有