
文章插图
大家好,小龙来为大家解答以上的问题 。定义一个复数类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重载运算符非成员非友元】本文到此分享完毕,希望对大家有所帮助 。
推荐阅读
-
汇总2023年支持无线充电的手机 无线充电手机排行榜前十名
-
-
-
池州市平天湖风景区致发建材经营部_工商信用信息_经营范围期限状态_法人_地址_注册资本_怎么样
-
-
-
-
-
天涯明月刀手游先人秘藏4怎么过-锦鲤先人秘藏4攻略2020新发布
-
厨房的纱窗油怎么清洗 不可拆卸厨房纱窗油污怎么清洗
-
-
大众2.0T330和380区别 大众20t330和380区别
-
-
-
2023年上映的恐怖片有哪些 2023恐怖片排行榜前十名电影
-
关于玛莎·费因斯简述 玛莎·费因斯
-
-
-
-
接下来可以躺平轻松赚大钱的3星座!他们胆识过人,可以轻取横财