溫馨提示×

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

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

C++--操作符重載 復(fù)數(shù)類

發(fā)布時(shí)間:2020-07-24 15:46:26 來(lái)源:網(wǎng)絡(luò) 閱讀:437 作者:淡淡_小孩 欄目:編程語(yǔ)言

一.操作符重載

Q:下面的復(fù)數(shù)解決方案是否可行?

class Complex
{
    public:
        int a;
        int b;
};

int main()
{
    Complex c1={1,2};
    Complex c2={3,4};

    Complex c3=c1+c2;

    return 0;
}

該段代碼想要實(shí)現(xiàn)的是將兩個(gè)復(fù)數(shù)類進(jìn)行相加得出第三個(gè)類
代碼實(shí)現(xiàn)的運(yùn)行結(jié)果
C++--操作符重載   復(fù)數(shù)類
由上面的結(jié)果圖可以得知,出現(xiàn)的錯(cuò)誤是無(wú)法匹配+號(hào)操作符的操作,同時(shí)出現(xiàn) 的潛在問(wèn)題是a與b是public成員,在實(shí)際的操作中應(yīng)將a與b設(shè)置為private成員
改正的代碼示例

#include <iostream>
using namespace std;

class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    int getA()
    {
        return a;
    }

    int getB()
    {
        return b;
    }

    friend Complex Add(const Complex& p1, const Complex& p2);
};

Complex Add(const Complex& p1, const Complex& p2)
{
    Complex ret;

    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;

    return ret;
}

int main()
{

    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = Add(c1, c2); // c1 + c2

    cout<<"c3.a ="<<c3.getA()<<endl; 
    cout<<"c3.b ="<<c3.getB()<<endl;

    return 0;
}

該代碼運(yùn)行了友元函數(shù)friend,同時(shí)定義了全局函數(shù)Add,將a與b設(shè)置為私有成員
運(yùn)行結(jié)果
C++--操作符重載   復(fù)數(shù)類
出現(xiàn)的疑問(wèn):Add函數(shù)可以解決Complex對(duì)象相加的問(wèn)題,但是Complex是現(xiàn)實(shí)世界中確實(shí)存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位和普通的實(shí)數(shù)相同---為什么不能讓+操作符也支持復(fù)數(shù)相加?
操作符重載
1.C++中的重載能夠擴(kuò)展操作符的功能
2.操作符的重載以函數(shù)的方式進(jìn)行
本質(zhì)--用特殊形式的函數(shù)擴(kuò)展操作符的功能
通過(guò)operator關(guān)鍵字可以定義特殊的函數(shù)
operator的本質(zhì)是通過(guò)函數(shù)重載操作符
語(yǔ)法
C++--操作符重載   復(fù)數(shù)類
操作符重載示例

#nclude <isotream>
using namespace std;

class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;

    return ret;
}
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // operator + (c1, c2)
    cout<<"c3.a ="<<c3.getA()<<endl; 
    cout<<"c3.b ="<<c3.getB()<<endl;

    return 0;
}

運(yùn)行結(jié)果如圖所示
C++--操作符重載   復(fù)數(shù)類
可以將操作符重載定義為類的成員函數(shù)
1.比全局操作符重載函數(shù)少一個(gè)參數(shù)
2.不需要依賴友元就可以完成操作符重載
3.編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)
C++--操作符重載   復(fù)數(shù)類
小結(jié)
1.操作符重載是C++的強(qiáng)大特性之一
2.操作符重載的本質(zhì)是通過(guò)函數(shù)擴(kuò)展操作符的功能
3.operator關(guān)鍵字是實(shí)現(xiàn)操作符重載的關(guān)鍵
4.操作符重載遵循相同的函數(shù)重載規(guī)則
5.全局函數(shù)和成員函數(shù)都可以實(shí)現(xiàn)對(duì)操作符的重載

二.完整復(fù)數(shù)類

復(fù)數(shù)類應(yīng)該具有的操作
C++--操作符重載   復(fù)數(shù)類
利用操作符重載
1.統(tǒng)一復(fù)數(shù)與實(shí)數(shù)的運(yùn)算方式
2.統(tǒng)一復(fù)數(shù)與實(shí)數(shù)的比較方式

    double getModulus();

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

復(fù)數(shù)類的實(shí)現(xiàn)
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::getModulus()
{
    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)//返回值是個(gè)引用
{
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }

    return *this;
}

Complex.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 getModulus();

    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

test.cpp

#include <stdio.h>
#include "Complex.h"

int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    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, 4);

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

輸出結(jié)果
C++--操作符重載   復(fù)數(shù)類
注意事項(xiàng)
1.C++規(guī)定賦值操作符(=)只能重載為成員函數(shù)
2.操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
3.操作符重載不能改變操作的個(gè)數(shù)
4.操作符重載不應(yīng)該改變操作符的原有語(yǔ)義

小結(jié)
1.復(fù)數(shù)的概念可以通過(guò)自定義類實(shí)現(xiàn)
2.復(fù)數(shù)中的運(yùn)算操作可以通過(guò)操作符重載來(lái)實(shí)現(xiàn)
3.賦值操作符只能通過(guò)成員函數(shù)實(shí)現(xiàn)
4.操作符重載的本質(zhì)為函數(shù)定義

向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