"Thinkinginc++" 33, various features of constructors and destructors

Source: Internet
Author: User

/*** 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" &LT;&LT;HEIGHT&LT;&LT;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

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.