C++全面細致講解復數(shù)類
更新時間:2022年06月01日 10:01:08 作者:清風自在 流水潺潺
本文章向大家介紹C++ 標準庫中的復數(shù)類,主要包括C++ 標準庫中的復數(shù)類使用實例、應用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下
一、復數(shù)類應該具有的操作
- 運算:+,- ,*,/
- 比較:== ,! =
- 賦值:=
- 求模:modulus
二、利用操作符重載
統(tǒng)一復數(shù)與實數(shù)的運算方式
統(tǒng)—復數(shù)與實數(shù)的比較方式
下面來看一下復數(shù)類的實現(xiàn):
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
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) { 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, 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++ 規(guī)定賦值操作符(=)只能重載為成員函數(shù)
- 操作符重載不能改變原操作符的優(yōu)先級
- 操作符重載不能改變操作數(shù)的個數(shù)
- 操作符重載不應改變操作符的原有語義
四、小結(jié)
- 復數(shù)的概念可以通過自定義類實現(xiàn)
- 復數(shù)中的運算操作可以通過操作符重載實現(xiàn)
- 賦值操作符只能通過成員函數(shù)實現(xiàn)
- 操作符重載的本質(zhì)為函數(shù)定義
到此這篇關(guān)于C++全面細致講解復數(shù)類的文章就介紹到這了,更多相關(guān)C++復數(shù)類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ LeetCode1796字符串中第二大數(shù)字
這篇文章主要為大家介紹了C++ LeetCode1796字符串中第二大數(shù)字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12C++的靜態(tài)聯(lián)編和動態(tài)聯(lián)編詳解
這篇文章主要介紹了C++的靜態(tài)聯(lián)編和動態(tài)聯(lián)編詳解,對于深入理解C++編譯運行原理有很大幫助,需要的朋友可以參考下2014-07-07