请高手---帮忙答题(C++面向对象)

来源:百度知道 编辑:UC知道 时间:2024/06/18 14:01:47
程序设计题:
1、设计表示一个复数的类COMPLEX(实部real、虚部imag为私有数据成员),包含以下公有函数成员或友元,并建立验证主函数:
⑴有参构造函数对数据成员初始化;
⑵有析构函数显示所释放的复数对象;
⑶能够显示该复数(如3+4i)
⑷重载复数的加+、减-运算,使用该类对象间能按复数的数学公式进行运算,且运算结果保存到复数对象中。

#include <iostream>
using namespace std;

class Complex
{
private:
int _real;
int _imag;
public:
Complex() { _real = 0; _imag = 0; }
Complex( int real, int imag ) { _real = real; _imag = imag; }
void output()
{
if ( _real != 0 )
{
if( _imag<0 )
{
cout<<_real<<_imag<<"i"<<endl;
}
else if ( _imag>0 )
{
cout<<_real<<"+"<<_imag<<"i"<<endl;
}
else
{
cout<<_real<<endl;
}
}
else if ( _real==0 )
{
if( _imag<0 )
{
cout<<_imag<<"i"<<endl;
}
else if