Complex
#include<iostream> #include<cmath> using namespace std; class complex{ public: complex(double x,double y); complex(complex &t); void add(complex &k); void show1(); double mod(); private: double real,imaginary; }; complex::complex(double x=0,double y=0){ real=x; imaginary=y; } //复制构造 complex::complex(complex &t){ real=t.real; imaginary=t.imaginary; } //加法 void complex::add(complex &k){ real=real+k.real; imaginary=imaginary+k.imaginary; } //输出 void complex::show1(){ cout<<real<<"+"<<imaginary<<"i"<<endl; } //取模 double complex::mod(){ return sqrt(real*real+imaginary*imaginary); } int main(){ complex c1(3,5); cout<<"c1="; c1.show1(); complex c2=4.5; cout<<"c2="; c2.show1(); complex c3(c1); cout<<"c3="; c3.show1(); c1.add(c2); cout<<"c1+c2="; c1.show1(); cout<<"|c1|="<<c1.mod(); return 0; }
可以运行,但有报错。
complex c2=4.5 可以这么用?
改complex c2(4.5)莫得问题。