- Instantiating derived-class object
- Chain of constructor calls
- Derived-class constructor invokes base class constructor
- Base of inheritance hierarchy
- Last constructor called in chain
- First constructor body to finish executing
- Example: Point3/Circle4/Cylinder hierarchy
Point3 constructor called last
Point3 constructor body finishes execution first
- Initializing data members
- Each base-class constructor initializes data members
Inherited by derived class
- Destroying derived-class object
- Chain of destructor calls
- Reverse order of constructor chain
- Destructor of derived-class called first
- Destructor of next base class up hierarchy next
- Continue up hierarchy until final base reached; After final base-class destructor, object removed from memory
- Base-class constructors, destructors, assignment operators
- Not inherited by derived classes
- Derived class constructors, assignment operators can call
- Constructors
- Assignment operators
Next example revisits the point/circle hierarchy by defining class Point4 (4.30-4.31) and class Circle5 (4.32-4.34) that contain constructors and destructors, each of which prints a message when it is invoked.
Figure 4.30:
Point4 class header file and Point4 base class contains a constructor
and a destructor. (part 1 of 2)
|
Figure 4.31:
Point4 base class contains a constructor and a destructor. (part 2of 2)
|
Figure 4.32:
Circle5 class header file.
|
Figure 4.33:
Circle5 class inherits from class Point4. (part 1 of 2)
|
Figure 4.34:
Circle5 class inherits from class Point4. (part 2 of 2)
|
Figure 4.35:
Constructor and destructor call order.
|
2004-07-29