About Virtual functions and their use ?
Answers were Sorted based on User's Feedback
Answer / sam
virtual function is used to overddin that function in
derived class
Is This Answer Correct ? | 46 Yes | 4 No |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Can you change the value of a variable while debugging a C# application?
What is difference between class and abstract class in c#?
When Doveloped C#
Can you declare struct members as protected?
How long can a string be c#?
What are modifiers in c#?
How does substring work in c#?
What is delegates in c#?
What is the value which is accepted by all data types ?
Write a program in c# to find the angle between the hours and minutes in a clock?
what is CSharp
What are the applications of c#?