Virtual functions
Why?
Answer:
Sometimes we want to override a base class method in child class.
As per example we want to provide a common function at top and leave the implementation on child class. Like if communicates is a method on Animal class then several child classes like human being ,Elephant all can have override this and implement their own medium in side it.
Hope all clear
Now lets take .net code example to undertstand things.
class Animal
{
public virtual void communicates()
{
Console.WriteLine ("communicates with each other ");
}
}
class DOG : Animal
{
public override void communicates ()
{
Console.WriteLine ("communicates with each other through barks "); }
}
class Human
{
public virtual void communicates()
{
Console.WriteLine("communicates with each other through language ");
}
}
class Program
{
static void Main()
{
//both Compile-time and run time type is Animal.
Animal ref1 = new Animal( );
ref1.communicates( );
// Compile-time type is Animal.
// Runtime type is Dog.
Animal ref2 = new Dog( );
ref2. communicates( ); }
}
show now we know what is virtual function and its uses now the second Question
How?
Answer:
It’s the compiler will write code to call the suitable version of the function at runtime
There must be some mapping information stored at complie time and some will be stored at run time right?
And that location is VTABLE. Typically there will be separate VTABLE of each class and complier will create it . On object creation a vpointer or VPTR to this VTABLE, is added as a hidden member of this object.Hence we can say Layout of VTABLE is fixed at compile time and then gets the address at runtime.
All VTABLEs will create in the data segment at startup and On object creation a VPTR is initialized which will get the address of VTABLE belongs to it.
Please come out with any questions if you have I will try my best
Why?
Answer:
Sometimes we want to override a base class method in child class.
As per example we want to provide a common function at top and leave the implementation on child class. Like if communicates is a method on Animal class then several child classes like human being ,Elephant all can have override this and implement their own medium in side it.
Hope all clear
Now lets take .net code example to undertstand things.
class Animal
{
public virtual void communicates()
{
Console.WriteLine ("communicates with each other ");
}
}
class DOG : Animal
{
public override void communicates ()
{
Console.WriteLine ("communicates with each other through barks "); }
}
class Human
{
public virtual void communicates()
{
Console.WriteLine("communicates with each other through language ");
}
}
class Program
{
static void Main()
{
//both Compile-time and run time type is Animal.
Animal ref1 = new Animal( );
ref1.communicates( );
// Compile-time type is Animal.
// Runtime type is Dog.
Animal ref2 = new Dog( );
ref2. communicates( ); }
}
show now we know what is virtual function and its uses now the second Question
How?
Answer:
It’s the compiler will write code to call the suitable version of the function at runtime
There must be some mapping information stored at complie time and some will be stored at run time right?
And that location is VTABLE. Typically there will be separate VTABLE of each class and complier will create it . On object creation a vpointer or VPTR to this VTABLE, is added as a hidden member of this object.Hence we can say Layout of VTABLE is fixed at compile time and then gets the address at runtime.
All VTABLEs will create in the data segment at startup and On object creation a VPTR is initialized which will get the address of VTABLE belongs to it.
Please come out with any questions if you have I will try my best