- 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 (11-12) and class Circle5 (13-15) that contain constructors and destructors, each of which prints a message when it is invoked.
Figure 11:
Point4 class header file and Point4 base class contains a constructor
and a destructor. (part 1 of 2)
|
Figure 12:
Point4 base class contains a constructor and a destructor. (part 2of 2)
|
Figure 13:
Circle5 class header file.
|
Figure 14:
Circle5 class inherits from class Point4. (part 1 of 2)
|
Figure 15:
Circle5 class inherits from class Point4. (part 2 of 2)
|
Figure 16:
Constructor and destructor call order.
|
2004-07-22