Q:下面的復(fù)數(shù)解決方案是否可行?
創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的宜黃網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
class Complex
{
public:
int a;
int b;
};
int main()
{
Complex c1={1,2};
Complex c2={3,4};
Complex c3=c1+c2;
return 0;
}
該段代碼想要實現(xiàn)的是將兩個復(fù)數(shù)類進(jìn)行相加得出第三個類
代碼實現(xiàn)的運行結(jié)果
由上面的結(jié)果圖可以得知,出現(xiàn)的錯誤是無法匹配+號操作符的操作,同時出現(xiàn) 的潛在問題是a與b是public成員,在實際的操作中應(yīng)將a與b設(shè)置為private成員
改正的代碼示例
#include
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = Add(c1, c2); // c1 + c2
cout<<"c3.a ="<
該代碼運行了友元函數(shù)friend,同時定義了全局函數(shù)Add,將a與b設(shè)置為私有成員
運行結(jié)果
出現(xiàn)的疑問:Add函數(shù)可以解決Complex對象相加的問題,但是Complex是現(xiàn)實世界中確實存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位和普通的實數(shù)相同---為什么不能讓+操作符也支持復(fù)數(shù)相加?
操作符重載
1.C++中的重載能夠擴(kuò)展操作符的功能
2.操作符的重載以函數(shù)的方式進(jìn)行
本質(zhì)--用特殊形式的函數(shù)擴(kuò)展操作符的功能
通過operator關(guān)鍵字可以定義特殊的函數(shù)
operator的本質(zhì)是通過函數(shù)重載操作符
語法
操作符重載示例
#nclude
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // operator + (c1, c2)
cout<<"c3.a ="<
運行結(jié)果如圖所示
可以將操作符重載定義為類的成員函數(shù)
1.比全局操作符重載函數(shù)少一個參數(shù)
2.不需要依賴友元就可以完成操作符重載
3.編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)
小結(jié)
1.操作符重載是C++的強大特性之一
2.操作符重載的本質(zhì)是通過函數(shù)擴(kuò)展操作符的功能
3.operator關(guān)鍵字是實現(xiàn)操作符重載的關(guān)鍵
4.操作符重載遵循相同的函數(shù)重載規(guī)則
5.全局函數(shù)和成員函數(shù)都可以實現(xiàn)對操作符的重載
復(fù)數(shù)類應(yīng)該具有的操作
利用操作符重載
1.統(tǒng)一復(fù)數(shù)與實數(shù)的運算方式
2.統(tǒng)一復(fù)數(shù)與實數(shù)的比較方式
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);
復(fù)數(shù)類的實現(xiàn)
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;
}
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
test.cpp
#include
#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é)果
注意事項
1.C++規(guī)定賦值操作符(=)只能重載為成員函數(shù)
2.操作符重載不能改變原操作符的優(yōu)先級
3.操作符重載不能改變操作的個數(shù)
4.操作符重載不應(yīng)該改變操作符的原有語義
小結(jié)
1.復(fù)數(shù)的概念可以通過自定義類實現(xiàn)
2.復(fù)數(shù)中的運算操作可以通過操作符重載來實現(xiàn)
3.賦值操作符只能通過成員函數(shù)實現(xiàn)
4.操作符重載的本質(zhì)為函數(shù)定義