complex类

#include<iostream>
#include<cmath>
using namespace std;
class complex{
    public:
        complex(double a,double b=0);
        complex(complex &p);
        void show();
        void madd(complex q);
        double mod();
    private:
        double zhen;
        double xu;
};
complex::complex(double a,double b)
{
    zhen=a;
    xu=b;
}
complex::complex(complex &p)
{
    zhen=p.zhen;
    xu=p.xu;
}
void complex::show()
{
    if(zhen!=0)
    {
        if(xu!=0)
            cout<<zhen<<‘+‘<<xu<<‘i‘<<endl;
        else
            cout<<zhen<<endl;
    }
    else
    {
        if(xu!=0)
            cout<<xu<<‘i‘<<endl;
        else
            cout<<0<<endl;
    }
}
void complex::madd(complex q)
{
    zhen+=q.zhen;
    xu+=q.xu;
}
double complex::mod()
{
    double x,y;
    x=zhen*zhen+xu*xu;
    y=sqrt(x);
    return y;
}
int main()
{
    complex c1(3,5);
    complex c2(4.5);
    complex c3(c1);
    c1.show();
    c2.show();
    c3.show();
    cout<<c3.mod()<<endl;
    c1.madd(c2);
    c1.show();
    return 0;
}

add在开始的时候想写在类的外面,然后发现不会写,想问老师如何调用类里面的数。

原文地址:https://www.cnblogs.com/0906mxyd/p/10629786.html

时间: 2024-11-08 00:44:57

complex类的相关文章

Complex 类的基本函数

//  Complex 类的基本函数 #include<iostream> using namespace std; class Complex { public : Complex(double real=1.0,double image=2.0)//构造函数  (有参) 调用2次拷贝构造 { _real=real; _image=image; } Complex()  //构造函数  (无参) {       _real=1.0;       _image=2.0; } 亦可写成: Com

std::complex类conj

std::conj(std::complex) std::conj(std::complex)用来计算complex的共轭部分,通过相反虚部的标识. example: 1 #include <iostream> 2 #include <complex> 3 4 int main() 5 { 6 std::complex<double> z(1,2); 7 std::cout << "The conjugate of " <<

设计、定义并实现Complex类

实验结论 Complex类 Code: #include<iostream> #include<cmath> #include<cstdlib> using namespace std; class Complex { public: Complex(double newr = 0, double newi = 0); Complex(Complex &c1); void add(Complex c1); void show(); double mod(); p

Complex 类的实现

编写复数类Complex,使得代码能够工作: 代码: #include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(double newrl=0,double newim=0) ;//默认值不能丢. Complex(Complex &c2); void add(Complex c2); void show(); double qumo(); private: do

课堂小练习: 设计、定义并实现complex类

#include<iostream> #include<cmath> using namespace std; class complex{ public: complex(double real0=0,double imaginary0=0); complex(complex &p); void add(complex); void qiumo(); void show(); private: double real; double imaginary; }; compl

课堂小练习(complex类)

定义一个复数类Complex,使得下面的代码能够工作: Complex c1(3,5);     //用复数3+5i初始化c1: Compex c2=4.5;      //用实数4.5初始化c2 c1.add(c2);              //将c1与c2相加,结果保存在c1中 c1.show();               //将c1输出(这时的结果应该是7.5+5i) cout<<c1.mod()      //c1.mod()对c1进行取模运算 1 #include <i

complex类 序列化时 IsRequired = true 问题

最近项目使用xpo作为数据底层, 需要在webservice中通信(传参和返回)直接使用xpo,则需要改造xpo类,自己实现IXmlSerializable接口. webservice客户端引用后, 调用时候发对象中的字符串类型的值不能为空,因为引用webservice生成的类字段上加了一个属性 [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, IsRequired=true)] 导致字段不能为空,

Complex 类

#include<iostream> #include<math.h> using namespace std; class Complex { public: Complex(double a, double b); Complex(double a) { real = a; i = 0; } Complex(Complex&p); void add(Complex a); void show(); double mod(); private: double real;

Complex类的实现

#include<iostream> #include<cmath> using namespace std; class complex{ public: complex(double a,double b=0); complex(complex &p); void show(); void madd(complex q); double mod(); private: double real; double imag; }; complex::complex(doubl