title: C++ Class Composition - When does constructors and destructors get executed
body: <p>PLEASE HELP! I know the question is lengthy, but I sincerely don't have anybody who can help me understand the concept.</p>
  
  <p>I have a code from a book, but I sincerely could not understand the output at all. The code is an example regarding two classes composition, and in what order the constructors and destructors are executed.</p>
  
  <p>Sorry for lengthy example:</p>
  
  <pre><code>// Fig. 10.8: Date.h
  // Date class definition; Member functions defined in Date.cpp
  #ifndef DATE_H
  #define DATE_H
  
  class Date
  {
      public:
      static const unsigned int monthsPerYear = 12; // number of months in a year
      explicit Date( int = 1, int = 1, int = 1900 ); // default constructor
      void print() const; // print date in month/day/year format
      ~Date(); // provided to confirm destruction order
  
  private:
      unsigned int month; // 1-12 (January-December)
      unsigned int day; // 1-31 based on month
      unsigned int year; // any year
  
      // utility function to check if day is proper for month and year
      unsigned int checkDay( int ) const;
  }; // end class Date
  
  #endif
  
  // Fig. 10.9: Date.cpp
  // Date class member-function definitions.
  #include &lt;array&gt;
  #include &lt;iostream&gt;
  #include &lt;stdexcept&gt;
  #include "Date.h" // include Date class definition
  using namespace std;
  
  // constructor confirms proper value for month; calls
  // utility function checkDay to confirm proper value for day
  Date::Date( int mn, int dy, int yr )
  {
      if ( mn &gt; 0 &amp;&amp; mn &lt;= monthsPerYear ) // validate the month
          month = mn;
      else
          throw invalid_argument( "month must be 1-12" );
  
      year = yr; // could validate yr
      day = checkDay( dy ); // validate the day
  
      // output Date object to show when its constructor is called
      cout &lt;&lt; "Date object constructor for date ";
      print();
      cout &lt;&lt; endl;
  } // end Date constructor
  
      // print Date object in form month/day/year
  void Date::print() const
  {
      cout &lt;&lt; month &lt;&lt; '/' &lt;&lt; day &lt;&lt; '/' &lt;&lt; year;
  } // end function print
  
  // output Date object to show when its destructor is called
  
  Date::~Date()
  {
      cout &lt;&lt; "Date object destructor for date ";
      print();
      cout &lt;&lt; endl;
  } // end ~Date destructor
  
  // utility function to confirm proper day value based on // month and     year; handles leap years, too
  unsigned int Date::checkDay( int testDay ) const
  {
      static const array &lt; int, monthsPerYear + 1 &gt; daysPerMonth =
          { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  
      // determine whether testDay is valid for specified month
      if ( testDay &gt; 0 &amp;&amp; testDay &lt;= daysPerMonth[ month ] )
          return testDay;
  
      // February 29 check for leap year
      if ( month == 2 &amp;&amp; testDay == 29 &amp;&amp; ( year % 400 == 0 || ( year % 4 == 0 &amp;&amp; year % 100 != 0 ) ) )
          return testDay;
  
      throw invalid_argument( "Invalid day for current month and year" );
  } // end function checkDay
  
  // Fig. 10.10: Employee.h
  // Employee class definition showing composition. // Member functions     defined in Employee.cpp.
  #ifndef EMPLOYEE_H
  #define EMPLOYEE_H
  
  #include &lt;string&gt;
  //#include "Date.h" // include Date class definition using namespace std;
  class Employee
  {
  public:
      Employee( const string &amp;, const string &amp;, const Date &amp;, const Date &amp; );
      void print() const;
      ~Employee(); // provided to confirm destruction order
  private:
      string firstName; // composition: member object
      string lastName; // composition: member object
      const Date birthDate; // composition: member object
      const Date hireDate; // composition: member object 23
  }; // end class Employee
  
  #endif
  
  // Fig. 10.11: Employee.cpp
  // Employee class member-function definitions.
  #include &lt;iostream&gt;
  #include "Employee.h" // Employee class definition
  #include "Date.h" // Date class definition
  using namespace std;
  
  // constructor uses member initializer list to pass initializer
  // values to constructors of member objects
  Employee::Employee( const string &amp;first, const string &amp;last, const Date &amp;dateOfBirth, const Date &amp;dateOfHire )
  : firstName( first ), lastName( last ), birthDate( dateOfBirth ), hireDate( dateOfHire )
  {
      // output Employee object to show when constructor is called
      cout &lt;&lt; "Employee object constructor: " &lt;&lt; firstName &lt;&lt; ' ' &lt;&lt; lastName &lt;&lt; endl;
  } // end Employee constructor
  
  // print Employee object
  void Employee::print() const
  {
      cout &lt;&lt; lastName &lt;&lt; ", " &lt;&lt; firstName &lt;&lt; " Hired: ";
      hireDate.print();
  
      cout &lt;&lt; " Birthday: ";
      birthDate.print();
  
      cout &lt;&lt; endl;
  } // end function print
  
  // output Employee object to show when its destructor is called
  Employee::~Employee()
  {
      cout &lt;&lt; "Employee object destructor: " &lt;&lt; lastName &lt;&lt; ", " &lt;&lt; firstName &lt;&lt; endl;
  } // end ~Employee destructor
  
  // Fig. 10.12: fig10_12.cpp
  // Demonstrating composition--an object with member objects.
  #include &lt;iostream&gt;
  //#include "Employee.h" // Employee class definition
  using namespace std;
  
  int main()
  {
      Date birth( 7, 24, 1949 );
      Date hire( 3, 12, 1988 );
      Employee manager( "Bob", "Blue", birth, hire );
  
      cout &lt;&lt; endl;
      manager.print();
  }//endmain
  </code></pre>
  
  <p>I know the code is too long, but really couldn't think of a way to do this easier. The output is below.</p>
  
  <pre><code>Date object constructor for date 7/24/1949
  Date object constructor for date 3/12/1988
  Employee object constructor: Bob Blue
  
  Blue, Bob  Hired: 3/12/1988  Birthday: 7/24/1949
  Employee object destructor: Blue, Bob
  Date object destructor for date 3/12/1988
  Date object destructor for date 7/24/1949
  Date object destructor for date 3/12/1988
  Date object destructor for date 7/24/1949
  </code></pre>
  
  <p>Now, my question is...
  Why is constructor of date object is only executed twice while the destructor is executed four times? To my understanding, the constructor of Date object should also be executed four times:
    1. Upon 'Date birth(7, 24, 1949)' in main
    2. Upon 'Date hire(3, 12, 1988)' in main
    3. Upon 'const Date birthDate' in Employee.h
    4. Upon 'const Date hireDate' in Employee.h</p>
  
  <p>The reason for my #3 and #4 is that I can't imagine Employee object constructor to copy 'birth' and 'hire' without same type of empty variables(or Date object in this case).
  So, my understanding is that in Employee header, upon private member Date object creation, explicit default Date constructor should be executed once for each of them (with 1/1/1900 as member variables mn, dy, yr).</p>