Which of the following statements is TRUE ?
2023
Which of the following statements is TRUE ?
- A.
Virtual functions do not implement polymorphism
- B.
Virtual functions do not permit calling of derived class functions using a base class pointer
- C.
We can never build an object from a class containing a pure virtual function
- D.
Pure virtual functions can never have a body
Attempted by 142 students.
Show answer & explanation
Correct answer: C
Answer: We can never build an object from a class containing a pure virtual function
Explanation: A class that declares at least one pure virtual function is an abstract class and cannot be instantiated directly. This means you cannot create objects of that class until a derived class overrides all pure virtual functions.
Why the statement about virtual functions not implementing polymorphism is wrong: Virtual functions are the mechanism for runtime (dynamic) polymorphism. Declaring a function virtual lets derived classes provide overrides that are selected at runtime through base-class pointers or references.
Why the statement about virtual functions not permitting calling derived functions via a base pointer is wrong: When a function is virtual, a call through a base-class pointer or reference will dispatch to the derived-class implementation if the object is of the derived type.
Why the statement that pure virtual functions can never have a body is wrong: C++ allows a pure virtual function to have a definition. However, even with a body, declaring a function pure virtual makes the class abstract until all pure virtuals are overridden in a concrete derived class.
Summary: The correct claim is that a class containing a pure virtual function is abstract and cannot be instantiated. Virtual functions enable polymorphism and virtual calls through base pointers will invoke derived overrides; pure virtual functions may have bodies but do not make the class instantiable until overridden.
A video solution is available for this question — log in and enroll to watch it.