主题:c++编程;复数运算符重载
下面是代码,运行不出来,请高手帮忙看看!!感激不尽。
题目是对复数运算符的重载
#include <iostream>
using namespace std;
class complex
{
public:
complex() ;
complex( float r, float img ) ;
complex(const complex& another );
complex operator +( const complex& another );
complex operator -(const complex& another );
complex operator *( const complex& another );
complex operator /( const complex& another );
complex operator ++(const complex& another );
complex operator --(const complex& another );
private:
float real, image;
};
int main()
{
complex c1( 2, 3 ), c2( 3, 3 );
complex c4, c5, c6, c7,c8,c9;
c4 = c1 + c2;
c5 = c1 - c2;
c6 = c1*c2;
c7 = c1/c2;
c8=c8++;
c9=c9--;
cout<<"c4="<<c4
<<"c5="<<c5
<<"c6="<<c6
<<"c7="<<c7
<<"c8="<<c8
<<"c9="<<c9;
return 0;
}
complex::complex()
{
}
complex::complex( float r, float img )
{
real = r; image = img;
}
complex::complex( const complex& another )
{
real = another.real;
image = another.image;
}
complex::complex operator +( const complex& another )
{
return complex( real+ another.real, image + another.image );
}
complex::complex operator -( const complex& another )
{
return complex( real- another.real, image - another.image );
}
complex::complex operator *( const complex& another )
{
complex prod;
prod.real = real*another.real - image*another.image;
prod.image = real*another.image + image*another.real;
return prod;
}
complex::complex operator /( const complex& another )
{
complex quot;
float sq = another.real*another.real + another.image*another.image;
quot.real = (real*another.real + image*another.image)/sq;
quot.image = (image*another.real - real*another.image)/sq;
return quot;
}
complex::complex operator ++(const complex& another)
{
another.real=1;
return complex(real+another.real,image);
}
complex::complex operator --(const complex& another)
{
another.real=1;
return complex(real-another.real,image);
}