设计一个复数类模板,包括两个复数的加减乘运算,一个复数和一个实部数值的加减乘,并进行测试。

#include

template
class Complex
{
private:
      T real;
      T image;
public:
      Complex(T x=0,T y=0);
      ~Complex()
      {}

      Complex Add(const Complex x) const;
      Complex Add(const T x) const;

      Complex Subtract(const Complex x) const;
      Complex Subtract(const T x) const;

      Complex Multiply(const Complex x) const;
      Complex Multiply(const T x) const;
      void show(void) const;
};

template
Complex::Complex(T x, T y)
{
      real=x;
      image=y;
}

template
Complex Complex::Add(const Complex x) const
{
      return Complex(real+x.real,image+x.image);
}
template
Complex Complex::Add(const T x) const
{
      return Complex(real+x,image);
}

template
Complex Complex::Subtract(const Complex x) const
{
      return Complex(real-x.real,image-x.image);
}
template
Complex Complex::Subtract(const T x) const
{
      return Complex(real-x,image);
}

template
Complex Complex::Multiply(const Complex x) const
{
      return Complex(real*x.real-image*x.image,real*x.image+x.real*image);
}
template
Complex Complex::Multiply(const T x) const
{
      return Complex(real*x,image);
}
template
void Complex::show(void) const
{
      cout<<" real is "<< real  ;
      cout<<" image is "<< image < }

 

#include"fushulei.h"

void main(void)
{
 Complex x(1.1f,1.1f),y(2.2f,2.2f),z,a,b;
 z=x.Add(y);
 cout<<"z is:";
 z.show();
 a=x.Subtract(y);
 cout<<"a is:";
 a.show();
 b=x.Multiply(y);
 cout<<"b is:";
 b.show();

 Complex u(1.111111,1.111111),v(2.222222,2.222222),w,c,d;
 w=u.Add(v);
 cout<<"w is:";
 w.show();
 c=u.Subtract(v);
 cout<<"c is:";
 c.show();
 d=u.Multiply(v);
 cout<<"d is:";
 d.show();
}
 

 
 

 

 

你可能感兴趣的:(模板,程序)