我們在之前已經(jīng)是實現(xiàn)了復(fù)數(shù)類的相加操作,那么我們今天就來完善下復(fù)數(shù)類。一個完整的復(fù)數(shù)類應(yīng)該具備的操作有:運算(+, -, *, /);比較(==, !=);賦值(=);求模(modulus);利用的就是操作符重載來統(tǒng)一實現(xiàn)復(fù)數(shù)與實數(shù)的運算和比較方式。復(fù)數(shù)類的實現(xiàn)如下

Comlpex.h 源碼
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
    double a;
    double b;
public:
    Complex(double a = 0, double b = 0);
    double getA();
    double getB();
    double modulus(const Complex& c);
    
    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);
    
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    
    Complex& operator = (const Complex& c);
};
#endifComplex.cpp 源碼
#include "Complex.h"
#include <math.h>
Complex::Complex(double a, double b)
{
    this->a = a;
    this->b = b;
}
double Complex::getA()
{
    return a;
}
double Complex::getB()
{
    return b;
}
double Complex::modulus(const Complex& c)
{
    return sqrt(a * a + b * b);
}
Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.b;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator / (const Complex& c)
{
    double cm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / cm;
    double nb = (b * c.a - a * c.b) / cm;
    Complex ret(na, nb);
    
    return ret;
}
bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}
bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)
{
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }
    
    return *this;
}test.cpp 源碼
#include <stdio.h>
#include "Complex.h"
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c2 - c1;
    Complex c4 = c1 * c3;
    Complex c5 = c2 / c1;
    
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
    printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
    
    Complex c6(2, 2);
    
    printf("c3 == c6 : %d\n", c3 == c6);
    printf("c3 != c4 : %d\n", c3 != c4);
    
    (c3 = c2) = c1;
    
    printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
    printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    return 0;
}我們在 test.cpp 中定義了兩個復(fù)數(shù),再接著利用相關(guān)操作定義了三個復(fù)數(shù),在第 16 行定義的復(fù)數(shù) c6,我們用口算都知道它和 c3 相等了,所以第 18 行會打印出 1,第 19 行也會打印出 1。第 21 行進(jìn)行的賦值操作,先是將 c2 賦值給 c3,然后再將 c1 賦值給它們的結(jié)果,也就是最后的結(jié)果是將 c1 賦值給 c3。我們看看編譯結(jié)果是否如我們所分析的那樣

我們看到編譯的結(jié)果和我們所分析的是一致的,至于乘法和除法的相關(guān)操作,我們可以自己去手動計算下,看看實現(xiàn)是否正確。
我們在實現(xiàn)操作符重載的時候得注意:a> C++ 規(guī)定賦值操作符(=)只能重載為成員函數(shù);b> 操作符重載不能改變原操作符的優(yōu)先級;c> 操作符不能改變操作數(shù)的個數(shù);d> 操作符重載不應(yīng)改變操作符的原有語義。
通過對復(fù)數(shù)類的完善的學(xué)習(xí),總結(jié)如下:1、復(fù)數(shù)的概念可以通過自定義類實現(xiàn);2、復(fù)數(shù)中的運算符操作可以通過操作符重載實現(xiàn);3、賦值操作符只能通過成員函數(shù)實現(xiàn);4、操作符重載的本質(zhì)為函數(shù)定義。
歡迎大家一起來學(xué)習(xí) C++ 語言,可以加我QQ:243343083。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
                當(dāng)前文章:完善的復(fù)數(shù)類(二十五)-創(chuàng)新互聯(lián)
                
                文章來源:http://www.chinadenli.net/article32/jispc.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站策劃、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
