Which of the following statements is TRUE ?
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.
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.
Let's carefully examine each given statement:
Statement 1: Virtual functions do not implement polymorphism
Statement 2: Virtual functions do not permit calling of derived class functions using a base class pointer
Statement 3: We can never build an object from a class containing a pure virtual function
Statement 4: Pure virtual functions can never have a body
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 |
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.
| 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. |
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.
Which statements is correct regarding main () function in C++?
I. It is a user defined function.
II. It is a pre-defined function.
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?
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:
If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?
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: