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