定义一个名为complex 的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数,并能打印复数

2025-03-07 11:13:18
推荐回答(2个)
回答1:

你的构造函数里面的赋值写反了应该是real=r;imag=i;。。粗心问题。。。

给好评哦,亲=。=

回答2:

#include
using namespace std;
class complex
{
double real, imag;
public:
complex(double r=0, double i = 0)
{
real = r;
imag = i;
}
complex(complex &other)
{
real += other.real;
imag += other.imag;
}
void show()
{
if(imag>0)
{
cout << real;
cout <<"+"<< imag << "i" << endl;}
else
{ cout << real;
cout << imag << "i" << endl;}

}
};
int main()
{
double m,n;
cout<<"输入复数的实部和虚部:"< cin>>m>>n;
complex c1(m, n);
complex c2(c1);
c1.show();
return 0;
}