Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose class Child is derived from class Parent that was in turn derived from c

ID: 3660177 • Letter: S

Question

Suppose class Child is derived from class Parent that was in turn derived from class GrandParent. This question concerns order of calls to constructors and destructors for these three classes. Declare an object of class Child. We know that Child, Parent and GrandParent constructors are called. In what order are these constructors are called? When the time comes to destroy the class Child object, we know that Child, Parent and GrandParent destructors are called. In what order are these destructor called?

Explanation / Answer

When a derived class object is created using constructors, it is created in the following order: 1) Virtual base classes are initialized, in the order they appear in the base list. 2) Nonvirtual base classes are initialized, in declaration order. 3) Class members are initialized in declaration order (regardless of their order in the initialization list). 4) The body of the constructor is executed. The destructors of base classes and members are called in the reverse order of the completion of their constructor: 1) The destructor for a class object is called before destructors for members and bases are called. 2) Destructors for nonstatic members are called before destructors for base classes are called. 3) Destructors for nonvirtual base classes are called before destructors for virtual base classes are called. For this simple example, this means that when we declare an object of class Child, the order of constructors will be: 1) GrandParent 2) Parent 3) Child. When the object of class Child is destroyed, the order of destructors will be: 1) Child 2) Parent 3) GrandParent.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote