Difficulty: Easy
Correct Answer: Both A and C
Explanation:
Introduction / Context:
Virtual dispatch enables runtime polymorphism by selecting the most-derived override of a function. However, not every class element can be declared virtual. This question checks which entities are excluded by the language rules.
Given Data / Assumptions:
Concept / Approach:
Constructors cannot be virtual because object type is known during construction and the vtable is not yet fully established for dispatch to an as-yet-unconstructed most-derived object. Data members cannot be virtual because they are not functions and do not participate in dynamic dispatch. Destructors, on the other hand, can and often should be virtual in polymorphic bases to ensure correct cleanup through base pointers.
Step-by-Step Solution:
1) Examine constructors: virtual keyword is disallowed → not virtual.2) Examine data members: only functions are virtual; data cannot be → not virtual.3) Examine destructors: may be declared virtual and are commonly used that way.4) Therefore, the correct combined answer is “Both A and C”.
Verification / Alternative check:
Attempt to compile a class with a virtual data member or a virtual constructor; compilers reject these constructs. A virtual destructor compiles and dispatches properly via base pointers.
Why Other Options Are Wrong:
Constructor alone or data member alone: incomplete; both are non-virtual by rule.Destructor: can be virtual; marking this as “cannot” is incorrect.Member functions (other than constructors): these can be virtual.
Common Pitfalls:
Confusing virtual constructors with factory patterns; factories simulate construction polymorphism without requiring a virtual constructor keyword.
Final Answer:
Both A and C
Discussion & Comments