Testing Classes Date and Employee

Figure 9.21 creates two Date objects (lines 10–11) and passes them as arguments to the constructor of the Employee object created in line 12. Line 15 outputs the Employee object’s data. When each Date object is created in lines 10–11, the Date constructor defined in lines 11–25 of Fig. 9.18 displays a line of output to show that the constructor was called (see the first two lines of the sample output). [Note: Line 12 of Fig. 9.21 causes two additional Date constructor calls that do not appear in the program’s output. When each of the Employee’s Date member objects is initialized in the Employee constructor’s member-initializer list (Fig. 9.20, lines 14–15), the default copy constructor for class Date is called. Since this constructor is defined implicitly by the compiler, it does not contain any output statements to demonstrate when it’s called.]


 1   // Fig. 9.21: fig09_21.cpp
 2   // Demonstrating composition--an object with member objects.
 3   #include <iostream>
 4   #include "Date.h" // Date class definition
 5   #include "Employee.h" // Employee class definition
 6   using namespace std;
 7
 8   int main()
 9   {
10      Date birth( 7, 24, 1949 );
11      Date hire( 3, 12, 1988 );
12      Employee manager( "Bob", "Blue", birth, hire );
13
14      cout << endl;
15      manager.print();
16   } // end main

Image

Fig. 9.21. Demonstrating composition—an object with member objects.

Class Date and class Employee each include a destructor (lines 34–39 of Fig. 9.18 and lines 33–37 of Fig. 9.20, respectively) that prints a message when an object of its class is destructed. This enables us to confirm in the program output that objects are constructed from the inside out and destroyed in the reverse order, from the outside in (i.e., the Date member objects are destroyed after the Employee object that contains them).

Notice the last four lines in the output of Fig. 9.21. The last two lines are the outputs of the Date destructor running on Date objects hire (Fig. 9.21, line 11) and birth (Fig. 9.21, line 10), respectively. These outputs confirm that the three objects created in main are destructed in the reverse of the order in which they were constructed. The Employee destructor output is five lines from the bottom. The fourth and third lines from the bottom of the output window show the destructors running for the Employee’s member objects hireDate (Fig. 9.19, line 21) and birthDate (Fig. 9.19, line 20). The last two lines of the output correspond to the Date objects created in lines 11and 10 of Fig. 9.21.

These outputs confirm that the Employee object is destructed from the outside in—i.e., the Employee destructor runs first (output shown five lines from the bottom of the output window), then the member objects are destructed in the reverse order from which they were constructed. Class string’s destructor does not contain output statements, so we do not see the firstName and lastName objects being destructed. Again, Fig. 9.21’s output did not show the constructors running for member objects birthDate and hireDate, because these objects were initialized with the default Date class copy constructors provided by the compiler.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.103.204