virtual modifier !!!

hi all .... plz can anybody explain the states of virtual modifier and plz with examples
i have been searching for that ... and i have now only one states and here it is


// cs_virtual_keyword.cs
using System;
class TestClass
{
public class Dimensions
{
public const double PI = Math.PI;
protected double x, y;
public Dimensions()
{
}
public Dimensions(double x, double y)
{
this.x = x;
this.y = y;
}

public virtual double Area()
{
return x * y;
}
}

public class Circle : Dimensions
{
public Circle(double r) : base(r, 0)
{
}

public override double Area()
{
return PI * x * x;
}
}

class Sphere : Dimensions
{
public Sphere(double r) : base(r, 0)
{
}

public override double Area()
{
return 4 * PI * x * x;
}
}

class Cylinder : Dimensions
{
public Cylinder(double r, double h) : base(r, h)
{
}

public override double Area()
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}

static void Main()
{
double r = 3.0, h = 5.0;
Dimensions c = new Circle(r);
Dimensions s = new Sphere(r);
Dimensions l = new Cylinder(r, h);
// Display results:
Console.WriteLine("Area of Circle = {0:F2}", c.Area());
Console.WriteLine("Area of Sphere = {0:F2}", s.Area());
Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
}
}



Answer this question

virtual modifier !!!

  • JMnet

    Ok. We're talking about the virtual member, right

    The Mark and boban have given good examples to show you how virtual method works and its use.

    I try to make it perfect.

    A virtual (Overridable in Visual Basic) member allows you to change a member's behavior by providing a different implementation of the member. They are typically used when you want a derived class of the type to handle the specifics of a given scenario.

    For example, the WebRequest class defines functionality for sending a request to any Universal Resource Identifier (URI). The FtpWebRequest class is a derived class of WebRequest that overrides its virtual methods to handle sending requests to URIs that use the File Transfer Protocol (FTP) scheme.

    Virtual members perform better than callbacks and events, but do not perform better than non-virtual methods.

    Do not make members virtual unless you have a good reason to do so and you are aware of all the costs related to designing, testing, and maintaining virtual members.

    Changing the implementation of a virtual member between versions can cause subtle version incompatibilities. For this reason, virtual methods are most costly to design correctly and test thoroughly.

    Do prefer protected accessibility over public accessibility for virtual members. Public members should provide extensibility (if required) by calling into a protected virtual member.

    Members needed for all scenarios that do not involve inheritance should be public.  

    Thank you



  • CS05pp2

    holy_spirit wrote:
    hi thank you .. but realy i cant understand what is the help that can VIRTUAL give
    or why i have to use it in my programs .... i see that you uses POLYMORPHISM in your example .... so i think there are a strong relation between POLYMORPHISM AND VIRTUAL
    plz more EXPLANATION i have 2 learn more
    BTW :oh man !!! u have such amazing space :D

    Everyone else is doing a good job, but let me add a little theory. Virtual functions are a mechanism for implementing polymorphism, as you guessed. Polymorphism means, if I recall correctly, "many forms." It refers to the fact that there can be many different forms of the same behavior in a hierarchy of classes. They all share the fact that they have the behavior, but each implements it in its own way. The classic example might be:

    class GraphicsPrimitive
    {
    public virtual void Draw();
    }

    class Rectangle : GraphicsPrimitive
    {
    public override void Draw();
    }

    Given something like this, and a few more classes such as Point, Ellipse, and Line, you could have a collection of Graphics object references. Each reference might actually refer to a Rectangle, a Line, whatever, but for the purposes of the collection they are all Graphics references, and can be treated as Graphics references. So you can loop through the collection and call Draw() on each one, and not care how the specific object actually implements Draw().

    HTH


  • BJHop

    Hi,

    the virtual modifier allows you to overwrite the functionality of a base classes method from a derived class. In the example below a method called About() in the Person class is marked as virtual, BUT the Man class which derives from Person also has an About() method, however it did not override the virtual method using the override keyword, so it is just hiding the method in the base class, so when we have a variable of type Man the mans version of the method will be called, however since it did not really override the base classes implementation of the method, if we refer to the Man instance with a variable of type Person then the Person classes version of about is still called, for example:

    using System;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Person p = new Person();
    Man m = new Man();

    //Will output: I am a person
    p.About();

    //Will output: I am a man
    m.About();

    Person p2 = m;

    //Will output: I am a person
    p2.About();
    }
    }

    class Person
    {
    public virtual void About()
    {
    Console.WriteLine("I am a person");
    }
    }

    class Man : Person
    {
    public void About()
    {
    Console.WriteLine("I am a man");
    }
    }
    }

    However, if we add the override keyword to the About() method i nthe Man class then it will overwrite the implementation in the base class (the base class implementation is still available in the Man class by calling base.About()) so that if an instance of the Man class is refered to by a variable of type Person still the man's version of About() will be called i.e.:

    using System;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Person p = new Person();
    Man m = new Man();

    //Will output: I am a person
    p.About();

    //Will output: I am a man
    m.About();

    Person p2 = m;

    //Will output: I am a man
    p2.About();
    }
    }

    class Person
    {
    public virtual void About()
    {
    Console.WriteLine("I am a person");
    }
    }

    class Man : Person
    {
    public override void About()
    {
    Console.WriteLine("I am a man");
    }
    }
    }

    That is a brief overview, is there something more specific about the virtual keyword that you wish to know about

    Mark.



  • Gy&#246&#59;rgy Bal&#225&#59;ssy

    Hi,

    an example would be the System.Windows.Forms.Form class, if you want to create your own form an do special graphical changes to the form, changing the way it is painted and displayed to the user then you can override the virtual OnPaint method in your own definition, you can then still pass your form around as a Form type variable. the main beneift of virtual functions ar ethat you can override the functionality of a method in a base class in a derived class, but still pass the derived class around in your application using a variable which is of the base type.

    Mark.



  • Prabu.

    hi thank you .. but realy i cant understand what is the help that can VIRTUAL give
    or why i have to use it in my programs .... i see that you uses POLYMORPHISM in your example .... so i think there are a strong relation between POLYMORPHISM AND VIRTUAL
    plz more EXPLANATION i have 2 learn more
    BTW :oh man !!! u have such amazing space :D

  • R.K.S.

    virtual gives you posibility to override member of base class. If you want to allow this you can mark that member as virtual. If you don't want to allow class that inherit from some base class to override that member than this member is not marked virtual.

  • virtual modifier !!!