您好,登錄后才能下訂單哦!
Q:下面的復數解決方案是否可行?
class Complex
{
public:
int a;
int b;
};
int main()
{
Complex c1={1,2};
Complex c2={3,4};
Complex c3=c1+c2;
return 0;
}
該段代碼想要實現的是將兩個復數類進行相加得出第三個類
代碼實現的運行結果
由上面的結果圖可以得知,出現的錯誤是無法匹配+號操作符的操作,同時出現 的潛在問題是a與b是public成員,在實際的操作中應將a與b設置為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;
}
該代碼運行了友元函數friend,同時定義了全局函數Add,將a與b設置為私有成員
運行結果
出現的疑問:Add函數可以解決Complex對象相加的問題,但是Complex是現實世界中確實存在的復數,并且復數在數學中的地位和普通的實數相同---為什么不能讓+操作符也支持復數相加?
操作符重載
1.C++中的重載能夠擴展操作符的功能
2.操作符的重載以函數的方式進行
本質--用特殊形式的函數擴展操作符的功能
通過operator關鍵字可以定義特殊的函數
operator的本質是通過函數重載操作符
語法
操作符重載示例
#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;
}
運行結果如圖所示
可以將操作符重載定義為類的成員函數
1.比全局操作符重載函數少一個參數
2.不需要依賴友元就可以完成操作符重載
3.編譯器優先在成員函數中尋找操作符重載函數
小結
1.操作符重載是C++的強大特性之一
2.操作符重載的本質是通過函數擴展操作符的功能
3.operator關鍵字是實現操作符重載的關鍵
4.操作符重載遵循相同的函數重載規則
5.全局函數和成員函數都可以實現對操作符的重載
復數類應該具有的操作
利用操作符重載
1.統一復數與實數的運算方式
2.統一復數與實數的比較方式
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);
復數類的實現
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 <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;
}
輸出結果
注意事項
1.C++規定賦值操作符(=)只能重載為成員函數
2.操作符重載不能改變原操作符的優先級
3.操作符重載不能改變操作的個數
4.操作符重載不應該改變操作符的原有語義
小結
1.復數的概念可以通過自定義類實現
2.復數中的運算操作可以通過操作符重載來實現
3.賦值操作符只能通過成員函數實現
4.操作符重載的本質為函數定義
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。