All Exams Test series for 1 year @ ₹349 only
Question

Which of the following statements is TRUE ?

The correct answer is

We can never build an object from a class containing a pure virtual function  

Let's analyze each statement to determine which one is TRUE regarding virtual functions and pure virtual functions in object-oriented programming.

Understanding Virtual Functions and Polymorphism

Virtual functions are a key feature in languages like C++ that enable runtime polymorphism. Polymorphism means "many forms," allowing an object to take on different behaviors depending on its actual type at runtime, even when accessed through a base class pointer or reference.

Analyzing Each Statement

Let's carefully examine each given statement:

Statement 1: Virtual functions do not implement polymorphism

  • This statement is FALSE.
  • Virtual functions are precisely the mechanism used to achieve runtime polymorphism in C++. When you call a virtual function using a pointer or reference to a base class object, the actual function executed is determined by the object's actual type at runtime, which could be a derived class type.

Statement 2: Virtual functions do not permit calling of derived class functions using a base class pointer

  • This statement is also FALSE.
  • The primary purpose of virtual functions is to allow calling the appropriate derived class function implementation when using a base class pointer or reference. This is the core concept of dynamic dispatch or runtime polymorphism. Without virtual functions, calling a method via a base pointer would always call the base class's implementation (or a static binding).

Statement 3: We can never build an object from a class containing a pure virtual function

  • This statement is TRUE.
  • A class that contains at least one pure virtual function is known as an abstract class.
  • A pure virtual function is declared by adding = 0; at the end of its declaration in the base class (e.g., virtual void myFunction() = 0;).
  • Abstract classes cannot be instantiated directly. This means you cannot create objects directly from an abstract class type. They serve as blueprints for derived classes, which must provide implementations for all pure virtual functions to become concrete classes (classes from which objects can be created).

Statement 4: Pure virtual functions can never have a body

  • This statement is FALSE.
  • While uncommon, pure virtual functions can have a definition (a body) provided separately.
  • Even if a body is provided, a derived class is still required to override the pure virtual function.
  • The body of a pure virtual function might be called by a derived class's implementation using the scope resolution operator (e.g., BaseClass::myPureVirtualFunction()), or in the destructor of the abstract base class. However, you cannot call it directly on an object of the abstract class type because you cannot instantiate it.

Based on the analysis, only Statement 3 is correct.

Statement Analysis Truth Value
Virtual functions do not implement polymorphism Virtual functions are essential for runtime polymorphism. FALSE
Virtual functions do not permit calling of derived class functions using a base class pointer Virtual functions allow calling derived class methods via base pointers (runtime polymorphism). FALSE
We can never build an object from a class containing a pure virtual function Classes with pure virtual functions are abstract classes, which cannot be instantiated. TRUE
Pure virtual functions can never have a body Pure virtual functions can have a body defined separately, although derived classes must still override them. FALSE

Conclusion on Virtual and Pure Virtual Functions

The correct statement is that you cannot create an object directly from a class that contains a pure virtual function. Such classes are abstract and serve as interfaces or base classes for further derivation.

Revision Table: Key Concepts

Concept Description
Virtual Function A member function declared in a base class with the virtual keyword, intended to be overridden in derived classes. Enables runtime polymorphism.
Pure Virtual Function A virtual function declared in a base class that has no definition within the base class. Declared with = 0; (e.g., virtual void draw() = 0;).
Abstract Class A class containing at least one pure virtual function. Cannot be instantiated (no objects can be created directly from it). Serves as a base class for concrete classes.
Concrete Class A class that can be instantiated (objects can be created from it). Must implement all pure virtual functions inherited from any abstract base classes.
Polymorphism The ability of an object to take on many forms. Runtime polymorphism in C++ is achieved using virtual functions, allowing dynamic binding.

Additional Information on Abstract Classes and Interfaces

Abstract classes are useful when you want to define a common interface for a group of related classes, but the base class itself is too general to have meaningful implementations for some functions. By declaring those functions as pure virtual, you force derived classes to provide their own specific implementations.

In some programming languages (like Java or C#), interfaces are similar concepts to abstract classes with only pure virtual functions and no data members (or only static/constant members). In C++, an abstract class with only pure virtual functions acts like an interface.

Was this answer helpful?

Important Questions from OOPs concept

  1. Which statements is correct regarding main () function in C++?

    I. It is a user defined function.

    II. It is a pre-defined function.

  2. Consider the following segment of C++ code.

    int j, n;

    for(j=1;j<=n; )

    j=j*2;

    What is the number of comparisons made in the execution of the loop for any n > 0?

  3. Given below are two statements:

    Statement (I): The friend function and the member functions of a friend class directly access the private and protected data.

    Statement (II): The friend function can access the private data through the member functions of the base class

    In the light of the above statements, choose the most appropriate answer from the options given below:

  4. If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?

  5. Match List I with List II

    LIST - I

    LIST - II

    A.

    If the implementation of generated or derived classes differ only through a parameter we have

    I.

    to use class hierarchy

    B.

    If the actual types of objects used cannot be known at compile time, then we have

    II.

    improved run-time efficiency

    C.

    If inline operations are essential and templates are used then we have

    III.

    to use templates

    D.

    To gain access to differing instances for derived classes through base we have

    IV.

    to use explicit casting

    Choose the correct answer from the options given below:

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App