complex类的定义和实现

#include
#include
using namespace std;
class complex
{
public:
    complex(double shi0 = 0, double xu0 = 0)
    {
        shi = shi0;
        xu = xu0;
    } 
    complex(complex &p);
    void add(complex &t);
    void show();
    double mod();
private:
    double shi;
    double xu;
};
complex::complex(complex &p)
{
    shi = p.shi;
    xu = p.xu;
}
void complex::add(complex &t)
{
    shi = shi + t.shi;
    xu = xu + t.xu;
}
void complex::show()
{
    cout << shi << '+' << xu << 'i' << endl;
}
double complex::mod()
{
    return sqrt((shi*shi) + (xu*xu));
}
int main()
{
    complex c1(3, 5);
    complex c2 = 4.5;
    complex c3(c1);
    c1.add(c2);
    c1.show();
    cout << c1.mod();
    return 0;
}

complex类的定义和实现_第1张图片

转载于:https://www.cnblogs.com/linjiahao1035/p/10638234.html

你可能感兴趣的:(complex类的定义和实现)