#include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(double x,double y); Complex(Complex &a); void add(Complex a); void show(); double mod(); private: double real; double imaginary; }; Complex::Complex(double x,double y=0){ real=x; imaginary=y; } Complex::Complex(Complex &a){ real=a.real; imaginary=a.imaginary; } void Complex::add(Complex a){ real=real+a.real; imaginary=imaginary+a.imaginary; } void Complex::show() { cout<<real<<‘+‘<<imaginary<<‘i‘<<endl; } double Complex::mod(){ double z; z=sqrt(real*real+imaginary*imaginary); return z; } int main(){ Complex c1(3,5); Complex c2(4.5); c1.add(c2); c1.show(); cout<<c1.mod()<<endl; return 0; }
小结:程序运行的时候一直有错误后来问了同学是Complex c2=4.5;出现了问题,应该是Complex c2(4.5);运用到了后面的知识。
实验二评论:
https://www.cnblogs.com/csl-40/p/10583521.html
https://www.cnblogs.com/Yyaoyyy/p/10594051.html
https://www.cnblogs.com/q1831726125/p/10597633.html
原文:https://www.cnblogs.com/fifi1224/p/10639198.html