Examine the example program named DATETIME.H for a practical example using multiple inheritance. You will notice that we are returning to our familiar date and time classes from earlier chapters.

There is a good deal to be learned from this very short header file since it is our first example of member initialization. There are two constructors for this class, the first being a very simple constructor that does nothing in itself as is evident from an examination of line 12. This constuctor allows the constructors to be executed for the classes new_date and time_of_day. In both cases a constructor will be executed that requires no parameters, and such a constructor is available for each of these two classes.

The second constuctor is more interesting since it does not simply use the default constructor, but instead passes some of the input parameters to the inherited class constructors. Following the colon in line 13 are two member initializers which are used to initialize members of this class. Since the two parent classes are inherited, they are also members of this class and can be initialized as shown. Each of the member initializers is actually a call to a constructor of the parent classes and it should be evident that there must be a constructor with the proper number of

input parameters to respond to the messages given. You will note that in line 14, we are actually calling the constructor with no parameters given explicitly. If we chose, we could simply let the system call that constructor automatically, but this gives us an explicit comment on what is happening.


Actually, we can use the member initializer to initialize class members also. If we had a class member of type int named member_var, we could initialize it also by mentioning the name of the member followed by the value we desired to initialize it to in parentheses. If we wished to initialize it to the value 13, we could use the following line of code in the member initializer list;


Following all member initialization, the normal constructor code is executed which in this case is given in line 16.


The order of member initialization may seem a bit strange, but it does follow a few simple rules. The order of member initialization does not follow the order given by the initialization list, but another very strict order over which you have complete control. All inherited classes are initialized first in the order they are listed in the class header. If lines 14 and 15 were reversed, class new_date would still be initialized first because it is mentioned first in line 8. It has been mentioned that C++ respects its elders and initializes its parents prior to itself. That should be a useful memory aid in the use of member initializers.

Next, all local class members are initialized in the order in which they are declared in the class, not the order in which they are declared in the initialization list. Actually, it would probably be good practice to not use the member initializer to initialize class members but instead to initialize them in the normal constructor code.

Finally, after the member initializers are all executed in the proper order, the main body of the constructor is executed in the normal manner.


The example program named USEDTTM.CPP uses the datetime class we just built, and like our previous examples, the main program is kept very simple and straight forward. You will note that the default constructor is used for the object named now, and the constructor with the member initializers is used with the objects named birthday and special.

The diligent student should have no trouble understanding the remaining code in this example.