About Virtual functions and their use ?

Answers were Sorted based on User's Feedback



About Virtual functions and their use ?..

Answer / sam

virtual function is used to overddin that function in
derived class

Is This Answer Correct ?    46 Yes 4 No

About Virtual functions and their use ?..

Answer / deepika

Second answer given here is correct to some extent.
If we use Virtual keywork next to a mehtod,it can be
overridden in derived classes,but it is not must to
override.

Is This Answer Correct ?    26 Yes 4 No

About Virtual functions and their use ?..

Answer / anand

If the instance function declared with a virtual modifier
then the function is called virtual function.

the derived class can give the redefinition to the virtual
function its optional.

Is This Answer Correct ?    26 Yes 7 No

About Virtual functions and their use ?..

Answer / sandeep

Virtual keyword is used to perform the dynamic binding
instead of early binding. To be brief below is the example

Class base
{
void display()
{
cout<<"in Base";
}
}
Class derived: public base
{
void display()
{
cout<<"in derived";
}
}
void main()
{
base* ptr;

ptr = new derived;
ptr->display();
}

When we execute above program output will be displayed as
"in base", but expected output was "in derived". So compiler
was choosing member function which matches the type of the
pointer rather than the member function which matches the
content of the pointer. But when we use Virtual keyword in
base class, it perform the dynamic binding, i.e., at run
time it choose the function to execute and executes "in
derived". So when we use virtual keyword, compiler will
select the member function which matches the content of the
pointer and it will ignore the member function which matches
the type of the pointer.

Is This Answer Correct ?    17 Yes 1 No

About Virtual functions and their use ?..

Answer / vishal sharma

Virtual Method are feature of object oriented programming
and used to Identify the Type of object that is calling any
particular method at the Run time so provides flexibility in
calling method.

Is This Answer Correct ?    17 Yes 2 No

About Virtual functions and their use ?..

Answer / tiger skumar

The Virtual is a keyword it used in the base class.When we
use the Virtual keyword in the function it must be override
in the inherited class with the same name.

Is This Answer Correct ?    35 Yes 21 No

About Virtual functions and their use ?..

Answer / vijay rana

when we override a metod in sub class,and we want to hide
the decelaration of the base class member then we used the
keyword virtual in superclass,
and keyword override in the subclass

Is This Answer Correct ?    15 Yes 3 No

About Virtual functions and their use ?..

Answer / deepak jindal

If a base class method is to be overriden, It is defined
using virtual keyword. You need to use the override keyword
in order to re-implement the virtual method. Exp:

public class Employee
{
public virtual void SetBasic(float money) //This method may
be overriden
{ Basic += money; }
}


public class Manager : Employee
{
public override void SetBasic(float money) //This method is
being overriden
{
float managerIncentive = 10000;
base.SetSalary(money + managerIncentive); //Calling base
class method
}
}

Is This Answer Correct ?    14 Yes 3 No

About Virtual functions and their use ?..

Answer / jyothish vakkom

intersection of 4 & 6

Is This Answer Correct ?    11 Yes 1 No

About Virtual functions and their use ?..

Answer / om shivaya namaha

Suppose we have 4 class

Class
Base (haveing method Display())
Class | Class
Parent1-------------------------------------------Parent2
(having method : Par1) (having method : Par2)
| |
-------------------------------------------------
|
Class Child( having Method :ch1)



as Parent1,Parent2 derived from the Base class the Method
Display will be inherited in both Parent Classes and the
Child class is inherited from parent1,parent2
so the Child class may contain Display(Base class Method)
method 2 time so it will create runtime problems to avoid
such problems we have to use Virtual Overide concepts


Class
Base (haveing method Virtual:Display())
Class | Class
Parent1-------------------------------------------Parent2
(having method : Par1) (having method : Par2)
Override : Display() Override : Display()
| |
-------------------------------------------------
|
Class Child( having Method :ch1)

then the Child class will contail only one Display method

Is This Answer Correct ?    9 Yes 2 No

Post New Answer

More C Sharp Interview Questions

What are the commonly used i/o classes?

0 Answers  


Which is faster abstract class or interface in c#?

0 Answers  


why sturcture ? why class?why you prefer structure and in which cases u go for class?

1 Answers   Microsoft,


Is c# used for any core features of windows vista?

0 Answers  


What is the difference between inheritance and abstract class?

0 Answers  






Can you store multiple data types in System.Array?

5 Answers  


what is the purpose of using statement in c#

0 Answers   Cognizant,


Is boxing an implicit conversion?

0 Answers  


Is void a class?

0 Answers  


Does a class need a constructor c#?

0 Answers  


Can struct be static in c#?

0 Answers  


What is Implementation inheritance

0 Answers  


Categories