OJ第六批——Problem L: B 构造函数和析构函数

问题及代码:

 

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

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 729   Solved: 476
[ 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部分的代码

 

#include <iostream> 
using namespace std; 
class Complex 
{ 
private: 
    double real; 
    double imag; 
public: 
    Complex(); 
    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(); 
}; 
  
Complex::Complex() 
{ 
    real=imag=0; 
} 
  
Complex::~Complex() 
{ 
    cout<<"destructed!"<<endl; 
} 
int main() 
{ 
   double real,image; 
  
   cin>>real>>image; 
   
   Complex  c1(real,image); 
  
   Complex  c2=c1; 
  
   return 0; 
} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值