溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

完善的復(fù)數(shù)類(二十五)

發(fā)布時(shí)間:2020-07-19 14:27:35 來(lái)源:網(wǎng)絡(luò) 閱讀:497 作者:上帝之子521 欄目:編程語(yǔ)言

        我們?cè)谥耙呀?jīng)是實(shí)現(xiàn)了復(fù)數(shù)類的相加操作,那么我們今天就來(lái)完善下復(fù)數(shù)類。一個(gè)完整的復(fù)數(shù)類應(yīng)該具備的操作有:運(yùn)算(+, -, *, /)比較(==, !=);賦值(=)求模(modulus);利用的就是操作符重載來(lái)統(tǒng)一實(shí)現(xiàn)復(fù)數(shù)與實(shí)數(shù)的運(yùn)算和比較方式。復(fù)數(shù)類的實(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);
};

#endif



Complex.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;
}

        我們?cè)?test.cpp 中定義了兩個(gè)復(fù)數(shù),再接著利用相關(guān)操作定義了三個(gè)復(fù)數(shù),在第 16 行定義的復(fù)數(shù) c6,我們用口算都知道它和 c3 相等了,所以第 18 行會(huì)打印出 1,第 19 行也會(huì)打印出 1。第 21 行進(jìn)行的賦值操作,先是將 c2 賦值給 c3,然后再將 c1 賦值給它們的結(jié)果,也就是最后的結(jié)果是將 c1 賦值給 c3。我們看看編譯結(jié)果是否如我們所分析的那樣

完善的復(fù)數(shù)類(二十五)

        我們看到編譯的結(jié)果和我們所分析的是一致的,至于乘法和除法的相關(guān)操作,我們可以自己去手動(dòng)計(jì)算下,看看實(shí)現(xiàn)是否正確。

        我們?cè)趯?shí)現(xiàn)操作符重載的時(shí)候得注意:a> C++ 規(guī)定賦值操作符(=)只能重載為成員函數(shù);b> 操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí);c> 操作符不能改變操作數(shù)的個(gè)數(shù);d> 操作符重載不應(yīng)改變操作符的原有語(yǔ)義。

        通過(guò)對(duì)復(fù)數(shù)類的完善的學(xué)習(xí),總結(jié)如下:1、復(fù)數(shù)的概念可以通過(guò)自定義類實(shí)現(xiàn);2、復(fù)數(shù)中的運(yùn)算符操作可以通過(guò)操作符重載實(shí)現(xiàn);3、賦值操作符只能通過(guò)成員函數(shù)實(shí)現(xiàn);4、操作符重載的本質(zhì)為函數(shù)定義。


        歡迎大家一起來(lái)學(xué)習(xí) C++ 語(yǔ)言,可以加我QQ:243343083

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI