Q:下面的復(fù)數(shù)解決方案是否可行?
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的宜黃網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(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;
}
該段代碼想要實(shí)現(xiàn)的是將兩個(gè)復(fù)數(shù)類(lèi)進(jìn)行相加得出第三個(gè)類(lèi)
代碼實(shí)現(xiàn)的運(yùn)行結(jié)果
由上面的結(jié)果圖可以得知,出現(xiàn)的錯(cuò)誤是無(wú)法匹配+號(hào)操作符的操作,同時(shí)出現(xiàn) 的潛在問(wèn)題是a與b是public成員,在實(shí)際的操作中應(yīng)將a與b設(shè)置為private成員
改正的代碼示例
#include <iostream>
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 ="<<c3.getA()<<endl;
cout<<"c3.b ="<<c3.getB()<<endl;
return 0;
}
該代碼運(yùn)行了友元函數(shù)friend,同時(shí)定義了全局函數(shù)Add,將a與b設(shè)置為私有成員
運(yùn)行結(jié)果
出現(xiàn)的疑問(wèn):Add函數(shù)可以解決Complex對(duì)象相加的問(wèn)題,但是Complex是現(xiàn)實(shí)世界中確實(shí)存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位和普通的實(shí)數(shù)相同---為什么不能讓+操作符也支持復(fù)數(shù)相加?
操作符重載
1.C++中的重載能夠擴(kuò)展操作符的功能
2.操作符的重載以函數(shù)的方式進(jìn)行
本質(zhì)--用特殊形式的函數(shù)擴(kuò)展操作符的功能
通過(guò)operator關(guān)鍵字可以定義特殊的函數(shù)
operator的本質(zhì)是通過(guò)函數(shù)重載操作符
語(yǔ)法
操作符重載示例
#nclude <isotream>
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 ="<<c3.getA()<<endl;
cout<<"c3.b ="<<c3.getB()<<endl;
return 0;
}
運(yùn)行結(jié)果如圖所示
可以將操作符重載定義為類(lèi)的成員函數(shù)
1.比全局操作符重載函數(shù)少一個(gè)參數(shù)
2.不需要依賴(lài)友元就可以完成操作符重載
3.編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)
小結(jié)
1.操作符重載是C++的強(qiáng)大特性之一
2.操作符重載的本質(zhì)是通過(guò)函數(shù)擴(kuò)展操作符的功能
3.operator關(guān)鍵字是實(shí)現(xiàn)操作符重載的關(guān)鍵
4.操作符重載遵循相同的函數(shù)重載規(guī)則
5.全局函數(shù)和成員函數(shù)都可以實(shí)現(xiàn)對(duì)操作符的重載
復(fù)數(shù)類(lèi)應(yīng)該具有的操作
利用操作符重載
1.統(tǒng)一復(fù)數(shù)與實(shí)數(shù)的運(yùn)算方式
2.統(tǒng)一復(fù)數(shù)與實(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ù)類(lèi)的實(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)//返回值是個(gè)引用
{
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 <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)
1.C++規(guī)定賦值操作符(=)只能重載為成員函數(shù)
2.操作符重載不能改變?cè)僮鞣膬?yōu)先級(jí)
3.操作符重載不能改變操作的個(gè)數(shù)
4.操作符重載不應(yīng)該改變操作符的原有語(yǔ)義
小結(jié)
1.復(fù)數(shù)的概念可以通過(guò)自定義類(lèi)實(shí)現(xiàn)
2.復(fù)數(shù)中的運(yùn)算操作可以通過(guò)操作符重載來(lái)實(shí)現(xiàn)
3.賦值操作符只能通過(guò)成員函數(shù)實(shí)現(xiàn)
4.操作符重載的本質(zhì)為函數(shù)定義
分享題目:C++--操作符重載復(fù)數(shù)類(lèi)
URL分享:http://jinyejixie.com/article36/pochpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、外貿(mào)建站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)