码迷,mamicode.com
首页 > 其他好文 > 详细

complex类

时间:2019-04-01 17:09:51      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:技术   img   void   mic   oid   iostream   pac   ble   space   

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

  技术图片

complex类

标签:技术   img   void   mic   oid   iostream   pac   ble   space   

原文地址:https://www.cnblogs.com/hongzai1206/p/10637325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!