当前位置:神舟问>百科知识>C++ 定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”........................

C++ 定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”........................

2023-10-16 14:18:30 编辑:join 浏览量:607

问题补充说明:定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”, 使之能用于复数的加,减,乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数的和,差,积,商。

C++ 定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”........................

#include<iostream>

usingnamespacestd;

classCComplex

{

private:

floatreal,image;

public:

CComplex(){}

CComplex(f采价放哪毫担片针督检loatr,floatimg){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;

}

CComplexoperator+(CComplex&another)

{

re来自turnCComplex(real+another.real,i360问答mage+another.image);

}

CComplexoperator-(CComplex&another)

{

returnCComplex(real-another.real,image-another.image);

}

CComplexoperator*(C准水笔赶Complex&another)

{

CComplexprod;

//prod=*this;

prod.real=real*anoth若承差er.real-image*another.image;

prod.image=real*another.image+image*another.real;

retu差适力程脚空罗和时曲言rnprod;

//returnCComplex(r友这势牛报易轻重车供eal+another械预吧掌围市依耐比值.real,image+another.i扩项题mage);

}

CComplexoperator/(CComplex&another)

{

CComplexquot;

f称果费loatsq=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;

returnquot;

}

};

voidmain()

{

CComplexc1(2,3),c2(3,3);

CComplexc4,c5,c6,c7;

c4=c1+c2;

c5=c1-c2;

c6=c1*c2;

c7=c1/c2;

}

标签:运算符,C++,Complex

版权声明:文章由 神舟问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.shenzhouwen.com/article/187324.html
热门文章