Composition: Objects as Members of Classes

An AlarmClock object needs to know when it is supposed to sound its alarm, so why not include a Time object as a member of the AlarmClock class? Such a capability is called composition. The program of Figs. 9-14 uses class Date and class Employee to demonstrate objects as members of other objects. The colon (:) in the header separates the member initializers from the parameter list. In Fig. 14, when each of the Employee's Date member object's initialized in the Employee constructor's member initializer list, the default copy constructor for class Date is called. This constructor is defined implicitly by the compiler and does not contain any output statements.
Figure 9: Date class definition.
\includegraphics[scale=0.43]{figures/img7.21.ps}
Figure 10: Date class member-function definitions. (part 1 of 2)
\includegraphics[scale=0.45]{figures/img7.22.ps}
\includegraphics[scale=0.45]{figures/img7.23.ps}
Figure 11: Date class member-function definitions. (part 2 of 2)
\includegraphics[scale=0.43]{figures/img7.24.ps}
Figure 12: Employee class definition showing composition.
\includegraphics[scale=0.45]{figures/img7.25.ps}
\includegraphics[scale=0.45]{figures/img7.26.ps}
Figure 13: Employee class member-function definitions,including constructor with a member-initializer list.
\includegraphics[scale=0.45]{figures/img7.27.ps}
\includegraphics[scale=0.45]{figures/img7.28.ps}
Figure 14: Member-object initializers.
\includegraphics[scale=0.45]{figures/img7.29.ps}
\includegraphics[scale=0.45]{figures/img7.30.ps}
2004-10-18