C + + Learning fundamentals 10--subclass constructors and destructors execution

Source: Internet
Author: User

1. Execution of the subclass constructor:

Executes the constructor of the parent class first, executes the constructor of the member object, and finally executes its own constructor.

When multiple classes are inherited, the constructors are executed in the same order as they were inherited, regardless of the order in which the subclass constructor calls the parent class constructor.

2. Execution of sub-class destructors:

Executes its own destructor, executes the destructor of the member object, and finally executes the destructor of the parent class.

When inheriting multiple classes, the execution of the parent class destructor is reversed from the execution of the constructor

3. The code is as follows:

1#include <iostream>2#include <string>3 4 using namespacestd;5 //the execution of a subclass constructor: Executes the constructor of the parent class, executes the constructor of the member object, and finally executes its own constructor6 //When multiple classes are inherited, the constructors are executed in the same order as they were inherited, regardless of the order in which the subclass constructor calls the parent class constructor.7 //the execution of a subclass destructor is performed by executing its own destructor, then executing the destructor of the member object, and finally executing the destructor of the parent class.8 //When inheriting multiple classes, the execution of the parent class destructor is reversed from the execution of the constructor9 Ten classBase1 One { A  Public: -BASE1 (intk) -     { theA =K; -cout<<"the Base1 constructor is called ...."<<Endl; -     }     -     voidTest () +     { -cout<<a<<Endl; +     } A~Base1 () at     { -cout<<"the destructor of the Base1 is called ...."<<Endl; -     } - Private: -     intA; - }; in  - classBase2 to { +  Public : -Base2 (intk) the     { *b =K; $cout<<"the Base2 constructor is called ...."<<Endl;Panax Notoginseng     } -     voidTest () the     { +cout<<b<<Endl; A     } the~Base2 () +     { -cout<<"the destructor of the BASE2 is called ...."<<Endl; $     } $ Private: -     intb;  - }; the  - classBASE3Wuyi { the  Public: - Base3 () Wu     { -c =0; Aboutcout<<"the default constructor for Base3 is called ...."<<Endl; $     } -     voidTest () -     { -cout<<c<<Endl; A     } +~Base3 () the     { -cout<<"the destructor of the BASE3 is called ...."<<Endl; $     } the Private: the     intC;  the }; the  - classDemos in { the  Public: theDemos (intk) About     { them =K; thecout<<"the demos constructor is called ...."<<Endl; the     } +     intGetm () -     { the         returnm;Bayi     } the~Demos () the     { -cout<<"the destructor of the demos is called ...."<<Endl; -     } the Private: the     intm;  the }; the  - classSon PublicBASE1, PublicBASE3, PublicBase2 the { the  Public: theSonintAintBintCintl);94     voidtest (); the~son (); the Private: the     ints;98 Demos D; About }; - 101Son::son (intAintBintCintl): Base1 (a), Base2 (b), S (c), D (L)102 {103cout<<"son's constructor is called ...."<<Endl;104 } the 106 voidson::test ()107 {108 base1::test ();109 base2::test (); the base3::test ();111cout<<s<<Endl; theCout<<d.getm () <<Endl;113 } theson::~Son () the { thecout<<"son's destructor is called ...."<<Endl;117 }118 119 intMain () - {121cout<<"====== The execution process of the sub-class constructor start====="<<Endl;122Son son (1,2,3,4);123cout<<"====== The execution process of the sub-class constructor end====="<<Endl;124 son.test (); the     126cout<<"the execution process of \n====== sub-class destructor ====="<<Endl;127     return 0; -}

The execution results are as follows:

C + + Learning fundamentals 10--subclass constructors and destructors execution

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.