Difficulty: Easy
Correct Answer: constructor
Explanation:
Introduction / Context:
Object construction is the first step in an object’s lifetime. C++ provides a family of constructors (default, parameterized, copy, move) to set up invariants and acquire resources. Identifying which mechanism runs at creation is foundational to RAII and exception safety patterns.
Given Data / Assumptions:
Concept / Approach:
At creation, C++ calls a suitable constructor based on the provided arguments (or none). The constructor establishes class invariants and initializes members (often via the member-initializer list). Virtual functions participate in runtime dispatch after construction; they are not “called to create” an object. Destructors run at the end of the lifetime, not the beginning. The global function main
is the program entry point and unrelated to individual object creation.
Step-by-Step Solution:
Verification / Alternative check:
Insert logging into each constructor. Logs appear exactly at creation time, confirming the role of constructors. Virtual calls during construction dispatch to the most-derived subobject constructed so far; they do not “create” the object.
Why Other Options Are Wrong:
Virtual function: ordinary member with dynamic dispatch, not used to create objects.
Destructor: used at destruction, not creation.
main: unrelated to per-object initialization.
Common Pitfalls:
Final Answer:
constructor
Discussion & Comments