其他分享
首页 > 其他分享> > 设计、定义并实现COMPLEX类

设计、定义并实现COMPLEX类

作者:互联网

#include<iostream>
#include<cmath>
using namespace std;
class Complex{
    public:
    Complex(double a=0,double b=0) ;
    Complex(Complex &x);
    void add(Complex x);
    void show();
    double mod();
    private:
        double imaginary,real;
}; 
Complex::Complex(double a,double b){
    real=a;
    imaginary=b; 
}
Complex::Complex(Complex &x){
    real=x.real;
    imaginary=x.imaginary;
}
void Complex::add(Complex c)
{
    real+=c.real;
    imaginary+=c.imaginary;
}
double Complex::mod()
{
    double m;
    m=sqrt(real*real+imaginary*imaginary);
    return m;
}
void Complex::show(){
    cout<<real<<"+"<<imaginary<<"i"<<endl;    
}
int main(){ 
    Complex c1(3,5);
    Complex x(4.5);
    Complex c3(c1);
    c1.add(x);
    c1.show();
    c3.show(); 
    cout<<c1.mod()<<endl;
    return 0;
}

 

标签:real,定义,show,double,void,COMPLEX,Complex,设计,imaginary
来源: https://www.cnblogs.com/rcloud/p/10646201.html