您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)C++中怎么實現(xiàn)操作符重載,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
各操作符重載的形式:
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);
代碼實現(xiàn):
test.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
test.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;
}
運行結(jié)果:
root@txp-virtual-machine:/home/txp/tt# ./a.out
c3.a = 2.000000, c3.b = 4.000000
c4.a = -6.000000, c4.b = 8.000000
c5.a = 3.000000, c5.b = 0.000000
c3 == c6 : 1
c3 != c4 : 1
c1.a = 1.000000, c1.b = 2.000000
c2.a = 3.000000, c2.b = 6.000000
c3.a = 1.000000, c3.b = 2.000000
上面設(shè)計到一些數(shù)學(xué)知識,比如復(fù)數(shù)的乘法和除法運算:
乘法:(a+bi)(c+di)=(ac-bd)+(bc+ad)i
除法:(a+bi)/(c+di) =(ac+bd)/c*c+d*d +(bc-ad)/c*c +d*d
3、注意事項:
C++規(guī)定賦值操作符"="只能重載為成員函數(shù)
操作符重載不能改變原操作符的優(yōu)先級
操作符重載不能改變操作數(shù)的個數(shù)
操作符重載不應(yīng)改變操作符的原有語義
二、總結(jié):
復(fù)數(shù)的概念可以通過自定義類實現(xiàn)
復(fù)數(shù)中的運算操作可以通過操作符重載實現(xiàn)
賦值操作符只能通過成員函數(shù)實現(xiàn)
操作符重載的本質(zhì)為函數(shù)定義
以上就是C++中怎么實現(xiàn)操作符重載,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。