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ù)的比較方式

下面來(lái)看一下復(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)改變操作符的原有語(yǔ)義
四、小結(jié)
- 復(fù)數(shù)的概念可以通過(guò)自定義類實(shí)現(xiàn)
- 復(fù)數(shù)中的運(yùn)算操作可以通過(guò)操作符重載實(shí)現(xiàn)
- 賦值操作符只能通過(guò)成員函數(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寫一個(gè)web服務(wù)器之基礎(chǔ)功能
C語(yǔ)言是一門很基礎(chǔ)的語(yǔ)言,程序員們對(duì)它推崇備至,本文將帶著大家來(lái)看一下,如何用C寫一個(gè)web服務(wù)器。2021-05-05
C++ LeetCode1796字符串中第二大數(shù)字
這篇文章主要為大家介紹了C++ LeetCode1796字符串中第二大數(shù)字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
C++的靜態(tài)聯(lián)編和動(dòng)態(tài)聯(lián)編詳解
這篇文章主要介紹了C++的靜態(tài)聯(lián)編和動(dòng)態(tài)聯(lián)編詳解,對(duì)于深入理解C++編譯運(yùn)行原理有很大幫助,需要的朋友可以參考下2014-07-07
C語(yǔ)言的分支和循環(huán)語(yǔ)句你真的了解嗎
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言的分支和循環(huán)語(yǔ)句,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
C++發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)代碼
這篇文章主要介紹了C++發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-06-06

