(c + +) constructors and destructors in C + + class inheritance

Source: Internet
Author: User


In the class inheritance of C + +,

When an object is created, the constructor of the base class is called first, and then the constructor of the next derived class is called, and so on;

When the object is reconstructed, its order is exactly the opposite of the construction;

#include <iostream>using namespace Std;class shape{public:void Draw () {cout<< "Base::D Raw ()" &LT;&LT;ENDL;}    void Erase () {cout<< "Base::erase ()" &LT;&LT;ENDL;}    Shape () {Draw ();} ~shape () {Erase ();}};/    /-------------------------------------------------class Polygon:public Shape{public:polygon () {Draw ();}    void Draw () {cout<< "Polygon::D Raw ()" &LT;&LT;ENDL;}    void Erase () {cout<< "Polygon Erase ()" &LT;&LT;ENDL;} ~polygon () {Erase ();}};/    /--------------------------------------------------class Rectangle:public Polygon{public:rectangle () {Draw ();}    void Draw () {cout<< "Rectangle::D Raw ()" &LT;&LT;ENDL;}    void Erase () {cout<< "Rectangle Erase ()" &LT;&LT;ENDL;} ~rectangle () {Erase ();}};/    /--------------------------------------------------class Square:public Rectangle{public:square () {Draw ();}    void Draw () {cout<< "Square::D Raw ()" &LT;&LT;ENDL;}    void Erase () {cout<< "Square Erase ()" &LT;&LT;ENDL;} ~square () {ErasE ();}};/    /--------------------------------------------------int main () {Polygon C;    Rectangle s;    Square T;    cout<< "------------------------------------------" <<endl; return 0;}

(c + +) constructors and destructors in C + + class inheritance

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.