C++-操作符重載、並實現複數類詳解

這篇文章主要介紹了C++-操作符重載、並實現複數類,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

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

函數重載

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章