YTU-OJ-构造函数和析构函数

来源:互联网 发布:免费信封打印软件 编辑:程序博客网 时间:2024/06/12 21:24

Problem E: B 构造函数和析构函数

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 511  Solved: 323
[Submit][Status][Web Board]

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部分的代码


/*    *Copyright (c)2015,烟台大学计算机与控制工程学院    *All rights reserved.      *作    者:单昕昕    *完成日期:2015年5月16日    *版 本 号:v1.0        */ #include <iostream>using namespace std;//将程序需要的其他成份写在下面,只提交begin到end部分的代码//******************** begin ********************class Complex{public:    Complex()    {        real=imag=0;    }    Complex(double r,double i)    {        real=r;        imag=i;        cout<<"("<<real<<","<<imag<<"i) is constructed!"<<endl;        cout<<"("<<real<<","<<imag<<"i) is copy constructed!"<<endl;    }    ~Complex( )    {        cout<<"destructed!"<<endl;    }private:    double real;    double imag;};//********************* end ********************int main(){    double real,image;    cin>>real>>image;    Complex c1(real,image);    Complex c2=c1;    return 0;}


0 0
原创粉丝点击