Complex类的实现
作者:互联网
Complex类的实现
#include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex (double a=0,double b=0); Complex (Complex &t); void add( Complex y); void show(); double mod(); private: double real; double imaginary; }; Complex::Complex(double a,double b){ real=a; imaginary=b; } Complex::Complex(Complex &t){ real=t.real; imaginary=t.imaginary; } void Complex::add(Complex y){ real=real+y.real; imaginary=imaginary+y.imaginary; } double Complex::mod(){ return sqrt(real*real+imaginary*imaginary); } void Complex::show(){ cout<<real<<"+"<<imaginary<<"i"<<endl; } int main (){ Complex c1(3,5); cout << "c1 = " ; c1.show(); cout <<"|c1| = "<<c1.mod()<<endl; Complex c2(4.5); cout << "c2 = "; c2.show(); cout << "c1 + c2 = "; c1.add(c2); c1.show(); return 0; }
结果
实验总结与体会
这次程序还是花了一个半小时,一开始构造函数出错,没有写初始值,然后重新修改,再从复制构造函数中找错。总之还是对C++的题目缺少练习。希望以后能能把程序做的又快又好。
标签:real,实现,double,void,Complex,imaginary,构造函数 来源: https://www.cnblogs.com/pink-fairy/p/10631582.html