Constructor and destructor of single inheritance derived class

Keywords: Java C C++ MySQL Eclipse

#include <iostream>
#include <cstdio> 
#include <cmath>
#include <climits>
#include <iomanip>
#include <windows.h>//
#include <iostream>
#define MAX(x,y) (x)>(y)?(x):(y)
#include <string.h>
using namespace std;
 class A
    {
      public:
        A()
        {  
            a=0;
            cout<<"A() Default constructor called."<<a<<endl;
        }
        A(int i)
        {
            a=i;
            cout<<"A(int )Constructor called."<<a<<endl;
        }
        ~A()
        {  cout<<"A Destructor called."<<a<<endl;  }
        void Print()
        {  cout<<a<<',';  }
        int Geta()
        {  return a;  }
      private:
       int a;
    };
    class B:public A
    {
      public:
        B()
        {  
            b=0;
            cout<<"B() Default constructor called."<<b<<endl;
        }
        B(int i,int j,int k):A(i),aa(j)
        {
            b=k;
            cout<<"B(int,int,int) Constructor called."<<b<<endl;
        }
        ~B()
        {  cout<<"B Destructor called."<<b<<endl;  }
        void Print()
        {
            A::Print();
            cout<<b<<','<<aa.Geta()<<endl;
        }
      private:
        int b;
        A aa;
    };
    int main()
    {
        B bb[2];
        bb[0]=B(7,8,9);
        bb[1]=B(12,13,14);
        for(int i=0;i<2;i++)
            bb[i].Print();
    }
The result is
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
A(int )Constructor called.7
A(int )Constructor called.8
B(int,int,int) Constructor called.9
B Destructor called.9
A Destructor called.8
A Destructor called.7
A(int )Constructor called.12
A(int )Constructor called.13
B(int,int,int) Constructor called.14
B Destructor called.14
A Destructor called.13
A Destructor called.12
7,9,8
12,14,13
B Destructor called.14
A Destructor called.13
A Destructor called.12
B Destructor called.9
A Destructor called.8
A Destructor called.7

The first is to tutor the basic knowledge.

1. The execution order of derived class constructors is as follows:

Priority 1: execute the base class constructor first

Priority 2: execute the constructor of sub objects (if there are sub objects)

Priority 3: finally, execute the function body of the derived class constructor

The execution order of the derived class destructor is as follows:

Priority 1: first execute the function body of the derived class destructor

Priority 2: execute the destructor of the class where the sub object is located (if there are sub objects)

Priority 3: finally, execute the destructor in the direct base class

In order to make it easier for you to understand, I have added some modifications to the output statement to make it easier for you to understand

We analyze the source code step by step. We can analyze part of the code first. Starting from the main function, let's look at B bb[2]. There are two objects in the array. The following code will be commented out first,

   int main()
    {
        B bb[2];
//        bb[0]=B(7,8,9);
//        bb[1]=B(12,13,14);
//        for(int i=0;i<2;i++)
//            bb[i].Print();
    }
Others remain unchanged. Output results
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
B Destructor called.0
A Destructor called.0
A Destructor called.0
B Destructor called.0
A Destructor called.0
A Destructor called.0

Change 1:

Here, we are confused about why there are two A() Default constructor called.0, followed by B() Default constructor called.0 instead of a A() Default constructor called.0, followed by B() Defaultconstructor called.0. There are two constructors in derived class B. the constructor with parameters displays the constructor containing the direct base class, The default constructor implies the constructor of the direct base class,   B (int i, int j, int k): we declare the object A aa in the derived class B of a (I), AA (J), so the constructor with parameters explicitly includes the constructor of the base class, that is, why do we output two A() Default constructor called.0 followed by B() Default constructor called.0,

   int main()
    {
        B bb[2];
        bb[0]=B(7,8,9);
        bb[1]=B(12,13,14);
//        for(int i=0;i<2;i++)
//            bb[i].Print();
    }
The result is
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
A() Default constructor called.0
A() Default constructor called.0
B() Default constructor called.0
A(int )Constructor called.7
A(int )Constructor called.8
B(int,int,int) Constructor called.9
B Destructor called.9
A Destructor called.8
A Destructor called.7
A(int )Constructor called.12
A(int )Constructor called.13
B(int,int,int) Constructor called.14
B Destructor called.14
A Destructor called.13
A Destructor called.12
B Destructor called.14
A Destructor called.13
A Destructor called.12
B Destructor called.9
A Destructor called.8
A Destructor called.7

Change 2:

Comment out the for loop, and the output result is as above. Why is the end 7, 8, 9, because we bb[0]=B(7,8,9)7 in front

A(int )Constructor called.7
A(int )Constructor called.8
B(int,int,int) Constructor called.9

After that, it will be destructed, that is, output

B Destructor called.9
A Destructor called.8
A Destructor called.7

Another bb[1] same thing

Output results: 7, 8, 9 because the code changes this position

B(int i,int j,int k):A(i),aa(j)
        {
            b=k;
            cout<<"B(int,int,int) Constructor called."<<b<<endl;
        }

  void Print()
        {
            A::Print();
            cout<<b<<','<<aa.Geta()<<endl;
        }

The last step is to destruct. The execution order of the destructor of the derived class is just opposite to that of the constructor of the derived class.

Posted by blackwidow on Tue, 16 Nov 2021 05:17:43 -0800