By declaring a base class function as virtual we allow the function to be overridden in subclasses

A) True
B) False
Explanation: If a function is declared virtual it needs to be overridden in subclass

Showing Answers 1 - 19 of 19 Answers

jay

  • Jan 3rd, 2006
 

Thats True. method declared virutal should be overridden in derived class.

  Was this answer useful?  Yes

samiksc

  • Jan 9th, 2006
 

The derived class should override a virtual function in case it wants to implement more / different functionality for the function.

  Was this answer useful?  Yes

Guest

  • Jan 9th, 2006
 

The derived class can provide new functionality by overriding the virtual method of the base class.

The method to be called is determined at the run-time depending on the type of the object calling the method, and it does not depend on the type of the variable declared to hold the object. The following example illustrates this point:

Suppose base class B and derived class D provide different implementations of a virtual method V(). Also suppose that there is a non-virtual method NV() of B which is overriden by D.

//In Main(), an object, obj, is declared to be of type B.

B obj;

//An object of D is created for initializing obj

obj = new D;

//Call virtual and non-virtual methods on obj:

obj.V(); //This call would execute D::F() since F() is virtual, and although obj is declared to be of type B, it contains object of type D.

obj.NV(); //This call would execute B::F(), looking at the declaration of obj.

This is the difference between virtual and plain overridable methods. Virtual methods execute slower than non-virtual ones because of run-time resolve of the method to be called. So programmer should be caeful while declaring a method as virtual.

  Was this answer useful?  Yes

Tduong

  • Aug 3rd, 2011
 

The following statement should be rephrased:

"If a function is declared virtual it needs to be overridden in subclass"

If a function is declared virtual it *can* be overridden in a subclass, but it does not necessarily *need* to be.

True but its not mandatory to override in subclass.. Virtual just gives an option & indicates that this function may or may not be override in subclass....

  Was this answer useful?  Yes

The answer is A) TRUE.

If you declare a virtual method in a class , its not mandatory to override it in the sub class.

However, In case of abstract class, the answer would be B) False since if you inherit from an abstract class, you would have to override all abstract methods from the base class in the sub-class.

  Was this answer useful?  Yes

Sivavt

  • Mar 29th, 2012
 

It is not mandatory for a derived class to override the virtual base method. Nevertheless, derived class can opt to override the virtual base method to redefine the functionality.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions