About Virtual functions and their use ?
Answer Posted / 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 |
Post New Answer View All Answers
Are arrays value types or reference types?
What is difference between string and string builder?
what is the purpose of using statement in c#
Is c# substring zero based?
Explain the types of assemblies in .net?
When was c# created?
Is there a way of specifying which block or loop to break out of when working with nested loops?
What is type class in c#?
Is 0 an unsigned integer?
What is the use of main method in c#?
What is difference between array and collection?
Why delegate is used in c#?
Write a C# program to find the Factorial of n
What is arraylist class in c#?
How do you create dlls in .NET