CSE687 - Object Oriented Design

Language Note #3
Implementing Initialization

Revised: 4/28/2011
Home Courses Code Handouts CoreTech Books Articles Math Research Masters Projects SWDev WebDev News OtherLinks SiteDesign Graduates AllPages Office Hours Notices

CSE681-SWMAA CSE686-IP CSE687-OOD CSE775-DO CSE776-DP CSE778-AWP CSE784-SWS

Cncp #01 Cncp #02 Cncp #03 Cncp #04 Cncp #05 Cncp #06 Cncp #07 Cncp #08 Cncp #09 Cncp #10 Cncp #11
Note #01 Note #02 Note #03 Note #04 Note #05 Note #06 Note #07 Note #08
Lang #01 Lang #02 Lang #03 Lang #04

Syllabus SG - Design SG - Templates SG - Class Relationships

Language Note #3:

In C++, initialization of all non-primative types is performed with constructors - functions with name the same as the class name. When an instance of a class is constructed by calling one of these functions the first thing that happens is that any members held by the class are constructed as are the base parts of the instance. Then the remaining initialization, perhaps to create an instance of a referenced type on the heap, is carried out in the body of the constructor.

Let's see how that works for classes with some interesting structure. Consider the diagram shown below. The Base class, B, holds an instance of a composed type C, presumably as a private or protected type. The class D inherits publicly from B and aggregates a reference to an instance of another class A.

image file not found

Conclusions for: Implementing Initialization

Always use initalization sequences when you design constructors, with the possible exception of void constructors (those with no arguments).
  1. Initialization sequences are sequences of constructor calls for a class's Base types and composed and aggregated member types.
  2. They let you, the designer, choose exactly how instances of your classes are constructed.