[YTU]_2625( 构造函数和析构函数)

来源:互联网 发布:类似日事清的软件 编辑:程序博客网 时间:2024/05/21 12:39

Description

在建立类对象时系统自动该类的构造函数完成对象的初始化工作,
当类对象生命周期结束时,系统在释放对象空间之前自动调用析构函数。

此题要求:
根据主程序(main函数)和程序执行结果,结合构造函数和析构函数相关知识,在下面的程序段基础上完成整个设计。

提示:(1)需要自定义复数类Complex,并在类中添加适当的构造函数和析构函数。
          (2)只提交begin到end部分的代码

#include <iostream>
using namespace std;

//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************


//********************* end ********************

int main()
{
double real,image;

cin>>real>>image;

Complex c1(real,image);

Complex c2=c1;

return 0;
}

程序输入输出样例如 Sample Input 和 Sample Output 所示。

Input

 一个复数的实部和虚部

Output

 调用相关构造函数和析构函数的运行结果(需要自己分析),参照Sample Output 所示。

Sample Input

1.5 2.6

Sample Output

(1.5,2.6i) is constructed!(1.5,2.6i) is copy constructed!destructed!destructed!

HINT

(1)需要自定义复数类Complex,并在类中添加适当的构造函数和析构函数。

(2)只提交begin到end部分的代码

#include <iostream>using namespace std;class Complex{public:    Complex(double r,double i):real(r),image(i)    {        cout<<'('<<real<<','<<image<<"i) is constructed!"<<endl;    }    ~Complex()    {        cout<<"destructed!"<<endl;    }       Complex(const Complex &c);private:    double real;    double image; }; Complex::Complex(const Complex &c)    {        real=c.real;        image=c.image;        cout<<'('<<real<<','<<image<<"i) is copy constructed!"<<endl;    }int main(){   double real,image;    cin>>real>>image;     Complex  c1(real,image);    Complex  c2=c1;    return 0;}

原创粉丝点击