1. 程式人生 > >完善的復數類(二十五)

完善的復數類(二十五)

C++ 復數類 賦值操作符 操作符重載

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


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

我們在 test.cpp 中定義了兩個復數,再接著利用相關操作定義了三個復數,在第 16 行定義的復數 c6,我們用口算都知道它和 c3 相等了,所以第 18 行會打印出 1,第 19 行也會打印出 1。第 21 行進行的賦值操作,先是將 c2 賦值給 c3,然後再將 c1 賦值給它們的結果,也就是最後的結果是將 c1 賦值給 c3。我們看看編譯結果是否如我們所分析的那樣

技術分享圖片

我們看到編譯的結果和我們所分析的是一致的,至於乘法和除法的相關操作,我們可以自己去手動計算下,看看實現是否正確。

我們在實現操作符重載的時候得註意:a> C++ 規定賦值操作符(=)只能重載為成員函數;b> 操作符重載不能改變原操作符的優先級;c> 操作符不能改變操作數的個數;d> 操作符重載不應改變操作符的原有語義。

通過對復數類的完善的學習,總結如下:1、復數的概念可以通過自定義類實現;2、復數中的運算符操作可以通過操作符重載實現;3、賦值操作符只能通過成員函數實現;4、操作符重載的本質為函數定義。


歡迎大家一起來學習 C++ 語言,可以加我QQ:243343083

完善的復數類(二十五)