Complex

#include
#include
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<"+"<"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_第1张图片

可以运行,但有报错。

complex c2=4.5 可以这么用?
改complex c2(4.5)莫得问题。

Complex_第2张图片

 

转载于:https://www.cnblogs.com/zxz2425405395/p/10631591.html

你可能感兴趣的:(Complex)