
文章插图
大家好,小龙来为大家解答以上的问题 。定义一个复数类complex重载运算符非成员非友元 , 定义一个复数类complex重载运算符这个很多人还不知道,现在让我们一起来看看吧!
1、#include
using namespace std;class CComplex{ private: float real, image; public: CComplex() { } CComplex( float r, float img ) { real = r; image = img; } CComplex( CComplex& another ) { real = another.real; image = another.image; } CComplex &operator = (CComplex &another ) { real = another.real; image = another.image; return *this; } CComplex operator +( CComplex & another ) { return CComplex( real+ another.real, image + another.image ); }CComplex operator -( CComplex & another ) { return CComplex( real- another.real, image - another.image ); }CComplex operator *( CComplex & another ) { CComplex prod; //prod = *this;prod.real = real*another.real - image*another.image; prod.image = real*another.image + image*another.real; return prod; //return CComplex( real+ another.real, image + another.image ); } CComplex operator /( CComplex & another ) { CComplex quot; float sq = another.real*another.real + another.image*another.image; quot.real = (real*another.real + image*another.image)/sq; quot.image = (image*another.real - real*another.image)/sq; return quot; }};void main(){ CComplex c1( 2, 3 ), c2( 3, 3 ); CComplex c4, c5, c6, c7; c4 = c1 + c2; c5 = c1 - c2; c6 = c1*c2; c7 = c1/c2;} 。
【定义一个复数类complex重载运算符 定义一个复数类complex重载运算符非成员非友元】本文到此分享完毕,希望对大家有所帮助 。
推荐阅读
-
-
-
-
-
-
-
对于一个不喜欢你的女生还要追吗 怎么跟女生说我不追你了
-
-
-
新车除甲醛异味最快最有效的方法 新车怎么除味除甲醛最快
-
-
-
人猿分界的重要标志是什么 人猿分界的一个重要标准是什么
-
女性激素6项不正常表现怎么治疗 女性激素6项不正常表现
-
-
擀面杖吹火 螳臂当车一手拿针一手拿线木匠戴木枷热锅上的蚂蚁【歇后语下一句】
-
简述达尔文自然选择学说的要点 简述达尔文自然选择学说主要内容
-
-
-