Which of the following is not correct for virtual function in C++?
Virtual function can be static.
Let's analyze the properties of virtual functions in C++ to determine which of the given statements is incorrect. Virtual functions are a key feature in C++ that enable runtime polymorphism, allowing you to call the correct version of a function based on the actual object type, even when accessed through a pointer or reference to a base class.
In C++, when you have a base class and derived classes, and you refer to a derived class object using a pointer or reference to the base class, calling a regular (non-virtual) member function will execute the version of the function defined in the base class (static binding). However, if the function in the base class is declared as virtual, and it is overridden in the derived class, calling the function through the base class pointer or reference will execute the version defined in the derived class (dynamic binding or runtime polymorphism).
This statement is not entirely accurate. While virtual functions are most commonly declared in the public section of a class because they are typically part of the class's interface intended for external use and polymorphism, they can also be declared in the protected or even private sections. Access rules still apply, but the virtual mechanism works regardless of the access specifier, as long as the function can be accessed where the call is made (e.g., within derived classes or friend functions for protected/private members). Therefore, stating they must be public is incorrect.
This statement is incorrect. A static member function in C++ belongs to the class itself, not to any specific object instance. It is called using the class name (e.g., ClassName::staticFunction()) and does not operate on an object. Virtual functions, on the other hand, are tied to object instances and rely on the object's dynamic type (determined at runtime) to decide which version of the function to execute. Since static functions are resolved at compile time and are not associated with an object's vtable (virtual table), they cannot be declared as virtual. The keywords static and virtual are mutually exclusive for a member function.
This statement highlights a common and essential use case for virtual functions to achieve polymorphism. While you can call a virtual function directly on an object of a specific type, the dynamic dispatch mechanism (calling the derived class version) only occurs when the virtual function is called through a pointer or a reference to the base class type that actually points/refers to a derived class object. Accessing via a base class object directly (not pointer/reference) leads to object slicing and static binding. Therefore, to leverage the power of runtime polymorphism, virtual functions are indeed typically accessed using pointers or references.
This statement is generally correct. A virtual function must be declared in the base class to establish the virtual interface that derived classes can override. The base class can provide a default definition for the virtual function, or it can declare it as a pure virtual function (e.g., virtual void myFunction() = 0;), making the base class abstract. In either case, the initial declaration (and often a definition) resides in the base class.
Based on the analysis, the statement that is incorrect regarding virtual functions in C++ is that a virtual function can be static. Static and virtual are incompatible concepts for a member function.
| Property | Correctness | Explanation |
|---|---|---|
| Declared virtual in base class | Generally Correct | Required for derived classes to override and enable polymorphism via base pointers/references. |
| Can be overridden in derived class | Correct | This is the primary purpose - to provide a specific implementation in derived classes. |
| Enables runtime polymorphism | Correct | Allows calling the correct function version based on the actual object type at runtime via base pointers/references. |
| Can be static | Incorrect | static functions belong to the class, not an object, and are resolved at compile time, unlike virtual functions which need object context and runtime resolution. |
| Can be accessed via base pointers/references for dynamic binding | Correct | This is how runtime polymorphism is typically achieved with virtual functions. |
| Can be pure virtual | Correct | A base class function can be declared virtual and = 0, making the base class abstract. |
Let A be the base class in C++ and B be the derived class from A with protected inheritance. Which of the following statement is false for class B?
Which of the following statements are true regarding C++?
(a) Overloading a function gives the capability to an existing operator to operate on other data types.
(b) Inheritance in object-oriented programming provides support to reusability.
(c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.
(d) Overloading is a type of polymorphism.
Choose the correct option from those given below:Match List I with List II
| List I | List II | ||
| (Programming Paradigm) | (Characteristic) | ||
| A. | Imperative | I. | Declarative, clausal representation, theorem proving |
| B. | Object‐oriented | II. | Side‐effect free, declarative, expression evaluation |
| C. | Logic | III. | Imperative, abstract data type |
| D. | Functional | IV. | Command‐based, procedural |
Choose the correct answer from the options given below :
A member function can always access the data in _______, (in C++).
Which of the following is correct (in C++)?