Q92.Marks: +2.0UGC NET Paper 2: Computer Science 7th Dec 2023 Shift 2
Which of the following statements is TRUE ?
1.Virtual functions do not implement polymorphism
2.Virtual functions do not permit calling of derived class functions using a base class pointer
3.We can never build an object from a class containing a pure virtual function ✓ Correct
4.Pure virtual functions can never have a body
Solution
The correct answer is We can never build an object from a class containing a pure virtual function.
Key Points
A pure virtual function is a function that is declared in a base class but does not have an implementation in that base class. Instead, it must be implemented in any non-abstract derived class.
The syntax for a pure virtual function usually looks something like this: virtual void MyPureVirtualFunction() = 0; The "= 0" is what makes the function "pure."
Abstract classes are classes that have at least one pure virtual function. The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.
Thus, an abstract class is basically a blueprint for other classes. It's not for creating objects directly, but for setting up a common standard for derived classes.
So, "We can never build an object from a class containing a pure virtual function." Because a class with a pure virtual function is an abstract class, and you can't directly instantiate an abstract class. You can only use them as base classes in inheritance structures.