"C + + Primer 15th" defines a derived class destructor

Source: Internet
Author: User

Learning materials

base class and derived class destructor execution order

Defining derived class destructors

Note : When you define an object, you call the constructor of the base class, and then call the constructor of the derived class, which is the exact opposite of invoking the destructor of the derived class, and then calling the destructor of the base class.

1#include <iostream>2 using namespacestd;3 4 classBase5 {6  Public:7 Base ()8     {9cout <<"Base contruction"<<Endl;Ten     } One     Virtual~Base () A     { -cout <<"Base Deconstruction"<<Endl; -     } the  - }; -  - classDerived: PublicBase + { -  Public: +Derived (inti) A     { atnum =i; -cout <<"Derived contruction"<< Num <<Endl; -     } -     Virtual~Derived () -     { -cout <<"Derived Deconstruction"<< Num <<Endl; in     } - Private: to     intnum; + }; -  the intMain () * { $Derived Derived (1);Panax Notoginseng  -base*baseptr; theBaseptr =NewDerived (2); + Delete baseptr; A}

Output Result:

"C + + Primer 15th" defines a derived class destructor

Related Article

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.