The construction order of the Child class parent class in C ++

Source: Internet
Author: User

(I am a newbie, new C ++) In general, the base class and the derived class (that is, the parent class and subclass) both contain Constructors (called when the class is created) and destructor (called when a class is deleted ).

What is the sequence of the constructor and destructor that a subclass calls when defining an object?

This is the code in Case 1:

# Include <iostream> using namespace STD; Class A {public: A () {cout <This <"New Super \ n ";}~ A () {cout <This <"delete super \ n" ;}}; Class B: Public A {public: B () {cout <This <"new sub \ n ";}~ B () {cout <This <"delete sub \ n" ;}}; int main (INT ARGs, char * argv []) {B; cout <& B <Endl;/** or: * B = new B (); cout <B <Endl; Delete B ;**/}

The result after running is:

It can be seen that when the initialization subclass is the first initialization parent class, and when the release is the first subclass, the parent class.

 

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.