Difficulty: Easy
Correct Answer: A virtual member function declared with = 0 that has no required implementation in the base class and must be overridden in derived classes.
Explanation:
Introduction / Context:
Pure virtual functions are a key concept in C++ object oriented programming. They are used to define interfaces and to create abstract base classes that cannot be instantiated directly. This question checks whether you understand what a pure virtual function is and how it differs from an ordinary virtual function in a class definition.
Given Data / Assumptions:
Concept / Approach:
A pure virtual function is a virtual function that is declared but not required to have an implementation in the base class. By writing = 0 at the end of the declaration, you tell the compiler that this function has no default behavior in the base class and must be provided by derived classes if those derived classes are to be concrete types. Any class that contains at least one pure virtual function becomes an abstract class, which means objects of that class cannot be created directly. Instead, you work with pointers or references to the base class that actually refer to instances of derived classes which implement the pure virtual functions.
Step-by-Step Solution:
Step 1: Recall that pure virtual functions are declared with = 0 in a class body.Step 2: Recognise that they express an interface requirement for derived classes.Step 3: Note that the base class does not need to supply a normal implementation, although C++ technically allows a definition if needed.Step 4: Compare the options and look for the one that mentions a virtual member function declared with = 0 and required to be overridden.Step 5: Option A matches this description, so it is the correct answer.
Verification / Alternative check:
Consider class Shape { public: virtual void draw() = 0; };. Shape is an abstract base class because draw is pure virtual. You cannot create Shape s; but you can derive classes such as Circle or Rectangle that provide void draw(). When you call draw through a Shape pointer or reference, C++ uses dynamic dispatch to select the correct derived implementation. This behaviour confirms that a pure virtual function provides a common interface without a required base implementation.
Why Other Options Are Wrong:
Option B focuses on return types and inlining rather than the = 0 syntax and interface requirement. Option C describes a non virtual function, which is unrelated. Option D confuses pure virtual functions with compile time features, while they are actually used at run time. Option E describes static member functions, which are not virtual and do not participate in polymorphism.
Common Pitfalls:
A common mistake is to think that a pure virtual function can never have a definition in the base class. C++ allows a definition, but the class remains abstract as long as the declaration uses = 0. Another pitfall is forgetting to override a pure virtual function in a derived class, which keeps the derived class abstract. Remember that pure virtual functions are mainly about designing clean interfaces and abstract base classes.
Final Answer:
A virtual member function declared with = 0 that has no required implementation in the base class and must be overridden in derived classes.
Discussion & Comments