C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類詳解
首先回憶下以前學(xué)的函數(shù)重載
函數(shù)重載
- 函數(shù)重載的本質(zhì)為相互獨(dú)立的不同函數(shù)
- 通過函數(shù)名和函數(shù)參數(shù)來確定函數(shù)調(diào)用
- 無法直接通過函數(shù)名得到重載函數(shù)的入口地址
- 函數(shù)重載必然發(fā)生在同一個(gè)作用域中
類中的函數(shù)重載
- 靜態(tài)成員函數(shù)能與普通成員函數(shù)建立重載關(guān)系
- 全局函數(shù)和成員函數(shù)不能構(gòu)成重載關(guān)系
操作符重載(operator)
什么是操作符重載?
大家都知道,在C里,有'+,-,*,/'這些操作符,且它們的功能就是實(shí)現(xiàn)普通變量運(yùn)算。
由于C++是面向?qū)ο蟮?遇到的變量大多都是對象,所以優(yōu)化了C里的操作符,使它們擁有了重載能力.能通過一定方式,使對象能進(jìn)行'+,-,*,/'等運(yùn)算.
操作符的重載是以函數(shù)的方式進(jìn)行.
操作符重載定義
操作符重載,通過operator關(guān)鍵字在函數(shù)前定義:
[返回類型] operator [需要重載的操作符](函數(shù)參數(shù))
{
//......
}
作符重載有幾種方式 : 全局操作符重載函數(shù)、全局操作符重載函數(shù)
編譯器首先會(huì)判斷運(yùn)算的若是對象,就會(huì)先從類里尋找成員操作符重載函數(shù),若沒找到,就會(huì)再去全局里尋找全局操作符重載函數(shù).
注意事項(xiàng):
- 操作符重載不能改變原操作符的優(yōu)先級
- 操作符重載不能改變操作數(shù)的個(gè)數(shù)
- 操作符重載的參數(shù)一般設(shè)為const class_name &類型(若只設(shè)為const class_name,會(huì)產(chǎn)生臨時(shí)對象)
- 在C++中,有些操作符必須需要有對象支持,所以只能為成員函數(shù).這種被稱為一元操作符
比如賦值(=)、下標(biāo)([])、下標(biāo)([])、調(diào)用(())和成員訪問箭頭(->):
Test t3=t2; //相當(dāng)于調(diào)用了: Test t3.operator =(t2); 里面會(huì)通過this指針來代替左側(cè)數(shù)t3
有些操作符既可以當(dāng)做成員操作符重載函數(shù),也可以當(dāng)做全局操作符重載函數(shù),由于函數(shù)參數(shù)可以多個(gè),便稱為二元操作符
比如加法(+),與(&&),或(||),逗號(,)等:
以加法(+)為例,當(dāng)設(shè)為全局操作符重載函數(shù)時(shí),執(zhí)行
Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 = operator +(t1,t2);
以加法(+)為例,當(dāng)設(shè)為成員操作符重載函數(shù)時(shí),執(zhí)行
Test t3=t1+t2; //相當(dāng)于調(diào)用了: Test t3 =t1.operator +(t2); //里面會(huì)通過this指針來代替左側(cè)數(shù)t1
多個(gè)重載的操作符重載函數(shù)
由于操作符重載函數(shù)帶參數(shù),所以可以存在多個(gè)相同的操作符重載函數(shù)
例如:
class Test
{
double x;
double y;
public:
Test operator +(const Test& t); //實(shí)現(xiàn)Test t3=t1+t2
Test operator +(int i); //實(shí)現(xiàn)Test t3=t1+1
Test operator +(double d); //實(shí)現(xiàn)Test t3=t1+1.25
//... ...
};
初步試驗(yàn)
1.接下來,來個(gè)全局操作符重載函數(shù)例子:
#include "stdio.h"
class Test{
int x;
int y;
public:
Test(int x=0,int y=0)
{
this->x=x;
this->y=y;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
friend Test operator + (const Test& t1,const Test& t2);
//聲明友元函數(shù),可以使用私有成員變量
};
Test operator + (const Test& t1,const Test& t2) //重載
{
Test ret;
ret.x=t1.x+t2.x;
ret.y=t1.y+t2.y;
return ret;
}
int main()
{
Test t1(1,3);
Test t2(2,4);
Test t3= t1 + t2; // 其實(shí)就是調(diào)用: Test t3 = operator +(t1,t2);
printf("t3.x:%d t3.y:%d\n",t3.getx(),t3.gety());
Test t4 =operator +(t1,t3); // t4 =t1 +t3
printf("t4.x:%d t4.y:%d\n",t4.getx(),t4.gety());
return 0;
}
打印結(jié)果:
t3.x:3 t3.y:7
t4.x:4 t4.y:10
換成成員操作符重載函數(shù)例子:
#include "stdio.h"
class Test{
int x;
int y;
public:
Test(int x=0,int y=0)
{
this->x =x;
this->y =y;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
Test operator + (const Test& t2)
{
Test ret;
ret.x = this->x + t2.x;
ret.y = this->y + t2.y;
return ret;
}
};
int main()
{
Test t1(1,3);
Test t2(2,4);
Test t3= t1 + t2; // 其實(shí)就是調(diào)用: Test t3 =t1.operator +(t2);
printf("t3.x:%d t3.y:%d\n",t3.getx(),t3.gety());
Test t4 =t1.operator +(t3); // t4 =t1 +t3
printf("t4.x:%d t4.y:%d\n",t4.getx(),t4.gety());
return 0;
}
打印結(jié)果:
t3.x:3 t3.y:7
t4.x:4 t4.y:10
加深理解
由于C++里,沒有復(fù)數(shù)的慨念,而在剛剛又學(xué)習(xí)了操作符重載,所以接下來便通過操作符重載來實(shí)現(xiàn)復(fù)數(shù)類
復(fù)數(shù)類應(yīng)該具有
兩個(gè)成員
實(shí)部a 、虛部b
運(yùn)算操作符
+ - : 結(jié)果 = 兩個(gè)實(shí)部進(jìn)行加減,兩個(gè)虛部進(jìn)行加減
* : 結(jié)果 = (a1+b1)(a2+b2)= (a1*a2 - b1*b2 )+( a2*b1 + a1*b2);
/ : 結(jié)果 =(a1+b1)/(a2+b2)= (a1*a2+b1*b2)/(a2* a2+b2* b2) +(b1*a2-a1*b2)/(a2* a2+b2* b2)
比較操作符:== ,!=
賦值操作符: =
求模成員函數(shù) : 等于a^2+b^2的算術(shù)平方根
所以復(fù)數(shù)類的操作符重載共有以下幾個(gè):

1.寫頭文件Complex.h:
#ifndef __COMPLEX_H
#define __COMPLEX_H
class Complex{
private:
double a;
double b;
public:
Complex(int a=0,int b=0);
Complex operator + (const Complex& t);
Complex operator - (const Complex& t);
Complex operator * (const Complex& t);
Complex operator / (const Complex& t);
bool operator == (const Complex& t);
bool operator != (const Complex& t);
Complex& operator = (const Complex& t);
double getModulus();
double getA();
double getB();
};
#endif
2.寫源文件Complex.cpp
#include "Complex.h"
#include "math.h"
Complex::Complex(int a,int b)
{
this->a = a;
this->b = b;
}
Complex Complex::operator + (const Complex& t)
{
Complex ret;
ret.a = a + t.a;
ret.b = b + t.b;
return ret;
}
Complex Complex::operator - (const Complex& t)
{
Complex ret;
ret.a = a - t.a;
ret.b = b - t.b;
return ret;
}
Complex Complex::operator * (const Complex& t)
{
Complex ret;
ret.a = (a* t.a - b* t.b );
ret.b = (t.a *b + a* t.b );
return ret;
}
Complex Complex::operator / (const Complex& t)
{
Complex ret;
ret.a = (a* t.a + b* t.b)/(t.a * t.a + t.b * t.b);
ret.b = (b* t.a - a* t.b)/(t.a * t.a + t.b * t.b);
return ret;
}
bool Complex::operator == (const Complex& t)
{
if((a== t.a)&&(b== t.b))
return true;
else
return false;
}
bool Complex::operator != (const Complex& t)
{
if((a!= t.a)||(b!= t.b))
return true;
else
return false;
}
Complex& Complex::operator = (const Complex& t)
{
if(this != &t)
{
a = t.a;
b = t.b;
}
return *this;
}
double Complex::getModulus()
{
return sqrt( a*a + b*b);
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
3.寫測試文件test.cpp
#include "stdio.h"
#include "Complex.h"
int main()
{
Complex t1(1,3);
Complex t2(2,6);
Complex t3=t1+t2;
printf("t3.a=%f t3.b=%f\n",t3.getA(),t3.getB());
printf("t3 Modulus:%f\n",t3.getModulus());
Complex t4=t3;
printf("t4==t3: %d\n",t4==t3);
printf("t4!=t3: %d\n",t4!=t3);
printf("t3==t1: %d\n",t3==t1);
return 0;
}
4.編譯運(yùn)行
t3.a=3.000000 t3.b=9.000000
t3 Modulus:9.486833
t4==t3: 1 //為真
t4!=t3: 0 //為假
t3==t1: 0 //為假
以上所述是小編給大家介紹的C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類詳解詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
初識(shí)C++?Vector模板與實(shí)例化原理
這篇文章主要為大家介紹了初識(shí)C++?Vector模板與實(shí)例化原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹
c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹;可供參考2012-11-11
Ubuntu16.04下配置VScode的C/C++開發(fā)環(huán)境
這篇文章主要介紹了Ubuntu16.04下配置VScode的C/C++開發(fā)環(huán)境的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C語言實(shí)現(xiàn)十六進(jìn)制轉(zhuǎn)換為十進(jìn)制的方法詳解
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)十六進(jìn)制轉(zhuǎn)換為十進(jìn)制的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11
Qt開發(fā)之使用socket實(shí)現(xiàn)遠(yuǎn)程控制
本篇文章將會(huì)介紹下位機(jī)通過心跳包和上位機(jī)之間進(jìn)行數(shù)據(jù)交互和遠(yuǎn)程功能控制的實(shí)現(xiàn)方法。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-11-11

