/*** book: "thinkinginc++" * functions: various features of constructors and destructors * Time: August 26, 2014 08:50:52* Author: cutter_point*//* Constructors and destructors are not return values. destructor: When the object is outside his scope, the compiler will call the destructor automatically, but the only evidence of the destructor call is the closing parenthesis that contains the object, and even if the goto statement is used to jump the destructor, it is still called */#include <iostream>using namespace Std;class tree{int height;//Private Member, height of this tree public:tree (int initialheight); The height of the given number of the destructor is assigned the initial value ~tree (); destructor, the void grow (int years) is automatically called; The height of the tree increases void printsize (); The high-size};//tree (int initialheight) of the output tree; The height of the destructor gives the initial value tree::tree (int initialheight) {height=initialheight;} ~tree (); Destructors are automatically called Tree::~tree () {cout<< "inside Tree destructor" <<endl; void PrintSize (); The output tree's high-size}//void grow (int years); The height of the tree is increased by void Tree::grow (int years) {height+=years; A}//void printsize () is added each year; High-size void tree for Output tree::p rintsize () {cout<< "tree height is" <<HEIGHT<<ENDL;} int main () {cout<< "before opening brace" <<endl; {Tree T (12); cout<< "After Tree creation"<<endl; T.printsize (); T.grow (4); cout<< "Before closing brace" <<endl; } cout<< "After closing brace" <<endl; return 0;}
"Thinkinginc++" 33, various features of constructors and destructors