式武神

导航

complex类的定义和实现

#include<iostream>
#include<cmath>
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;
}

posted on 2019-04-01 19:06  式武神  阅读(319)  评论(1编辑  收藏  举报