设计、定义并实现Complex类
2019-03-31 16:24 Chirly 阅读(87) 评论(0) 编辑 收藏 举报设计、定义并实现Complex类
#include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(double real1=0,double imaginary1=0); Complex(Complex &x); void add(Complex y); void show(); double mod(); private: double real; double imaginary; }; Complex::Complex(double real1,double imaginary1):real(real1),imaginary(imaginary1){}; Complex::Complex(Complex &x):real(x.real),imaginary(x.imaginary){}; void Complex::add(Complex y) { real=real+y.real; imaginary=imaginary+y.imaginary; } void Complex::show() { cout<<real<<"+"<<imaginary<<"i"<<endl; } double Complex::mod() { double s; s=sqrt(real*real+imaginary*imaginary); return s; } int main() { Complex c1(3,5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod()<<endl; return 0; }
总结:对构造函数掌握不是很好,程序实现过程困难,通过此次complex实验加深了印象,需要花更多时间努力。