complex 类

//定义一个复数类Complex,使得下面的代码能够工作。
//Complex c1(3,5)//用复数3+5i初始化c1
//complex c2=4.5//用实数4.5初始化c1
//complex c3(c1)//用c1初始化c3
//c1.add(c2)//将c1和c2相加,结果保存在c1中
//c1.show()//将c1输出(在屏幕上显示7.5+5i)
//cout<//c1.mod()c1进行取模运算


#include 
#include 
using namespace std;
class Complex
{
public:
    Complex(double a=0,double b=0);
    Complex(Complex &p);
    void show();
    void add(Complex c1);
    double mod();
private:
    double real;
    double imaginary;
};

Complex::Complex(double a,double b)
{
    real=a;
    imaginary=b;
}
Complex::Complex(Complex &p)
{real=p.real;
    imaginary=p.imaginary;
}
void Complex::add(Complex c1)
{real=real+c1.real;
    imaginary=imaginary+c1.imaginary;
}
void Complex::show()
{cout<"+"<"i"<<endl;}
double Complex::mod()
{
   return (real*real+imaginary*imaginary);

}
int main(int argc, const char * argv[]) {
    Complex c1(3,5);
    cout<<"c1=";
    c1.show();
    cout<<"|c1|="<endl;
    Complex c2(4.5);
    cout<<"c2=";
    c2.show();
    
    cout<<"c1+c2=";
    c1.add(c2);
    c2.show();
    return 0;

complex 类_第1张图片

小结:

感觉还是有很多地方不太通畅,在朋友帮助下学会的,可能是因为练习的太少了叭,然后就不太熟练,导致花时间太久。

 

转载于:https://www.cnblogs.com/yinyinzuinihai/p/10645977.html

你可能感兴趣的:(complex 类)