我們在之前已經(jīng)是實(shí)現(xiàn)了復(fù)數(shù)類的相加操作,那么我們今天就來完善下復(fù)數(shù)類。一個完整的復(fù)數(shù)類應(yīng)該具備的操作有:運(yùn)算(+, -, *, /);比較(==, !=);賦值(=);求模(modulus);利用的就是操作符重載來統(tǒng)一實(shí)現(xiàn)復(fù)數(shù)與實(shí)數(shù)的運(yùn)算和比較方式。復(fù)數(shù)類的實(shí)現(xiàn)如下
在成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報的無錫營銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)十多年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(liá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" #includeComplex::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#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)操作,我們可以自己去手動計(jì)算下,看看實(shí)現(xiàn)是否正確。
我們在實(shí)現(xiàn)操作符重載的時候得注意:a> C++ 規(guī)定賦值操作符(=)只能重載為成員函數(shù);b> 操作符重載不能改變原操作符的優(yōu)先級;c> 操作符不能改變操作數(shù)的個數(shù);d> 操作符重載不應(yīng)改變操作符的原有語義。
通過對復(fù)數(shù)類的完善的學(xué)習(xí),總結(jié)如下:1、復(fù)數(shù)的概念可以通過自定義類實(shí)現(xiàn);2、復(fù)數(shù)中的運(yùn)算符操作可以通過操作符重載實(shí)現(xiàn);3、賦值操作符只能通過成員函數(shù)實(shí)現(xiàn);4、操作符重載的本質(zhì)為函數(shù)定義。
歡迎大家一起來學(xué)習(xí) C++ 語言,可以加我QQ:243343083。