C++全面細(xì)致講解復(fù)數(shù)類
一、復(fù)數(shù)類應(yīng)該具有的操作
- 運(yùn)算:+,- ,*,/
- 比較:== ,! =
- 賦值:=
- 求模:modulus
二、利用操作符重載
統(tǒng)一復(fù)數(shù)與實(shí)數(shù)的運(yùn)算方式
統(tǒng)—復(fù)數(shù)與實(shí)數(shù)的比較方式

下面來看一下復(fù)數(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);
};
#endifComplex.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é)果如下:

三、注意事項(xiàng)
- C++ 規(guī)定賦值操作符(=)只能重載為成員函數(shù)
- 操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
- 操作符重載不能改變操作數(shù)的個(gè)數(shù)
- 操作符重載不應(yīng)改變操作符的原有語義
四、小結(jié)
- 復(fù)數(shù)的概念可以通過自定義類實(shí)現(xiàn)
- 復(fù)數(shù)中的運(yùn)算操作可以通過操作符重載實(shí)現(xiàn)
- 賦值操作符只能通過成員函數(shù)實(shí)現(xiàn)
- 操作符重載的本質(zhì)為函數(shù)定義
到此這篇關(guān)于C++全面細(xì)致講解復(fù)數(shù)類的文章就介紹到這了,更多相關(guān)C++復(fù)數(shù)類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c++將vector迭代器轉(zhuǎn)換為指針的實(shí)現(xiàn)方式
這篇文章主要介紹了c++將vector迭代器轉(zhuǎn)換為指針的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
VSCode遠(yuǎn)程代碼開發(fā)及DNS隧道端口轉(zhuǎn)發(fā)實(shí)現(xiàn)遠(yuǎn)程辦公代碼
這篇文章主要介紹了VSCode遠(yuǎn)程代碼開發(fā)及DNS隧道端口轉(zhuǎn)發(fā)實(shí)現(xiàn)遠(yuǎn)程辦公,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
簡(jiǎn)要對(duì)比C語言中三個(gè)用于退出進(jìn)程的函數(shù)
這篇文章主要介紹了C語言中三個(gè)用于退出進(jìn)程的函數(shù)的對(duì)比,分別為_exit()函數(shù)和on_exit()函數(shù)以及atexit()函數(shù),需要的朋友可以參考下2015-08-08
C++基于消息隊(duì)列的多線程實(shí)現(xiàn)示例代碼
這篇文章主要給大家介紹了關(guān)于C++基于消息隊(duì)列的多線程實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

