C++高手速进!数据结构题~!

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:40:50
实现第1.4.3.3节的C++类Complex,设计一个模板函数Test实现x+y*z,另设计函数main,分别以整数、浮点数和复数调用函数Test,来测试该函数和Complex类。

运行过了,没有错误!
#include <iostream.h>
class Complex
{
private:
double real,imag;

public:
Complex(double r=0.0,double i=0.0);
void print();
Complex operator+(Complex c);
Complex operator*(Complex c);
};

Complex::Complex(double r, double i )
{
real=r;
imag=i;
}

Complex Complex::operator+(Complex c)
{
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}

Complex Complex::operator*(Complex c)

{ Complex temp;
temp.real=real*c.real-imag*c.imag;
temp.imag=real*c.imag+imag*c.real;
return temp;
}

void Complex::print()
{
cout<<real;
if (imag>0) cout<<"+";
if (imag!=0) cout<<imag<<"i\n";
}

template <class T>