16th Week OJ Brush problem--problem e:b Constructors and destructors

Source: Internet
Author: User

Description

When the class object is established, the system automatically completes the initialization of the object by the constructor of the class.
When the class object life cycle ends, the system automatically calls the destructor before releasing the object space.

This topic requires:
According to the main program (main function) and program execution results, combined with the constructor and destructor related knowledge, the following program section based on the completion of the entire design.

Tip: (1) You need to customize the plural class complex, and add the appropriate constructors and destructors to the class.
(2) The code that only submits the begin to end section

Input

The real and imaginary parts of a complex number

Output

Call the run results of the related constructors and destructors (which you need to analyze yourself), as shown in 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. * File name: Test.cpp * Chen Dani * Completion date: June 26, 2015 * Version number: v1.0 */#include <iostream>using namespace Std;class complex{priv ATE:    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;}


Learning experience: This problem is the construction function and destructors, to master the definition of constructor overloading and the definition of destructors. Keep trying!!!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

16th Week OJ Brush problem--problem e:b constructors and destructors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.