日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++中数学运算、比较、赋值操作符的重载

發(fā)布時間:2025/4/5 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中数学运算、比较、赋值操作符的重载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1 C++中數(shù)學(xué)運(yùn)算、比較、賦值操作符的重載
      • 1.1 完善的復(fù)數(shù)類

1 C++中數(shù)學(xué)運(yùn)算、比較、賦值操作符的重載

1.1 完善的復(fù)數(shù)類

復(fù)數(shù)類應(yīng)該具有的操作:

  • 運(yùn)算:+,-,*,/
  • 比較:==、!=
  • 賦值:=
  • 求模:modulus

我們需要利用操作符重載:

  • 統(tǒng)一復(fù)數(shù)與實數(shù)的運(yùn)算方式。
  • 統(tǒng)一復(fù)數(shù)與實數(shù)的比較方式。

    復(fù)數(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; }

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

參考資料:

  • C++深度解析教程
  • 《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的C++中数学运算、比较、赋值操作符的重载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。