zoukankan      html  css  js  c++  java
  • 第十六周oj刷题——Problem E: B 构造函数和析构函数

    Description

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

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



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

    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!

    /* All rights reserved.
     * 文件名:test.cpp
     * 作者:陈丹妮
     * 完毕日期:2015年 6 月 26 日
     * 版 本 号:v1.0
     */
    #include <iostream>
    using namespace std;
    class Complex
    {
    private :
        double real;
        double imag;
    public:
        Complex(double r,double i):real(r),imag(i)
        {
            cout<<"("<<real<<","<<imag<<"i)"<<" is constructed!"<<endl;
        }
        Complex(Complex &c)
        {
            real=c.real;
            imag=c.imag;
            cout<<"("<<real<<","<<imag<<"i)"<<" is copy constructed!"<<endl;
        }
        ~Complex()
        {
            cout<<"destructed!"<<endl;
        }
    };
    
    int main()
    {
        double real,image;
        cin>>real>>image;
        Complex c1(real,image);
        Complex c2=c1;
        return 0;
    }
    


    学习心得:这道题考的是构造函数和析构函数,要掌握定义构造函数重载和析构函数的定义。继续努力!

    。!

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    基于分布式锁解决定时任务重复问题
    基于Redis的Setnx实现分布式锁
    基于数据库悲观锁的分布式锁
    使用锁解决电商中的超卖
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7324537.html
Copyright © 2011-2022 走看看