#include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(double a1,double b1); Complex(double a1); Complex(Complex &c); void add(Complex d); void show(); double mod(); private: double a,b; }; Complex::Complex(double a1,double b1) {a=a1; b=b1; } Complex::Complex(double a1) {a=a1; } Complex::Complex(Complex &c) {a=c.a; b=c.b; } void Complex::add(Complex d) {a=a+d.a; b=b+d.b; } void Complex::show() {cout<<a<<"+"<<b<<"i\n"; } double Complex::mod() {double m; m=sqrt(a*a+b*b); return m; } int main() { Complex c1(3,5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }
作业小结:
通过老师的讲解和同学的帮助下,对构造函数和析构函数有了一个明显的了解。
对于复数的应用也越发熟练
在同学的帮助下,把程序编写出来了
https://www.cnblogs.com/rcloud/p/10604345.html#4220073
https://www.cnblogs.com/0906mxyd/p/10604795.html#4220066
https://www.cnblogs.com/qiuxiuh/p/10599381.html