问题及代码:
#include <iostream>
using namespace std;
//******************** begin ********************
class Complex
{
public:
Complex(double r=0,double i=0);
~Complex()
{
cout<<"destructed!"<<endl;
}
private:
double real;
double image;
};
Complex ::Complex(double r,double i)
{
real=r;
image=i;
cout<<"("<<real<<","<<image<<"i) is constructed!"<<endl;
cout<<"("<<real<<","<<image<<"i) is copy constructed!"<<endl;
}
//********************* end ********************
int main()
{
double real,image;
cin>>real>>image;
Complex c1(real,image);
Complex c2=c1;
return 0;
}
运行结果:
本文探讨了C++中复数类的构造函数实现,包括默认构造和带参数构造,以及如何实现复制构造函数以正确复制复数对象。
6797

被折叠的 条评论
为什么被折叠?



