类的练习

#include<iostream>
#include<cmath>
using namespace std;
class complex
{
public:
    complex(double x1,double y1);
 complex(double x1);
    complex(complex &c1);
    void add(complex c1);
 void show();
 double mod1();
private:
 double x;
 double y;
};
complex::complex(double x1,double y1)
{
 x=x1;
 y=y1;
}
complex::complex(double x1)
{
 x=x1;
 y=0;
}
complex::complex(complex &c1)
{
 x=c1.x;
    y=c1.y;
}
void complex::add(complex c1)
{
 x=x+c1.x;
 y=y+c1.y;
}
void complex::show()
{
 cout<<x<<"+"<<y<<"i"<<endl;
}
double complex::mod1()
{
 double m;
    m=x*x+y*y;
 return sqrt(m);
}
int main()
{  
 complex c1(3,5);
    complex c2(4.5);
    complex c3(c1);
    c1.add(c2);
    c1.show();
    cout<<c1.mod1();
    return 0;
}
 

 

 
 
posted @ 2019-03-30 16:15  与风无关  阅读(120)  评论(1编辑  收藏  举报