定义复数类,并在主函数定义类的对象进行调用测试。
要求:① 数据成员为双精度类型的实部和虚部。
② 具有无参和有参的构造函数。
③ 具有深拷贝构造函数。
④ 具有析构函数。
⑤ 具有输出函数、求模函数、加法函数等。
C++类与对象 怎样利用复数类算复数的模等运算?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- threenewbee 2018-11-14 19:11关注
#include <iostream> #include <math.h> using namespace std; class Complex { private: double real; double img; public: Complex() { real = img = 0.0; } Complex(double r, double i) { real = r; img = i; } Complex(const Complex &c) { real = c.real; img = c.img; } ~Complex() { cout << "dector has been called!" << endl; } void Output() { cout << real << "+" << img << "i" << endl; } Complex operator +(const Complex &c) { return Complex(real + c.real, img + c.img); } double GetModulus() { return sqrt(img * img + real * real); } }; int main() { Complex c1, c2(1.0, 2.0), c3(1.5, 2.5); c1 = c2 + c3; c1.Output(); Complex c4 = c1; c4.Output(); cout << c4.GetModulus() << endl; return 0; }
如果问题得到解决,请点我回答左上角的采纳和向上的箭头
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog