virtual member function....

Discussion in 'C++' started by jakeriggs, Apr 30, 2008.

  1. jakeriggs

    jakeriggs New Member

    Joined:
    Apr 23, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.oceanhillsrecovery.com/
    Hi

    I am very new to this.......
    somebody can explain well the use of virtual member function....???
    and
    The difference between how virtual and non-virtual member functions are called???


    thanks....
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    You can search on google for detail Or ask some point where you are unable to understand.
    In short:

    In c++ two types of polymorphism One is Compile time : By function overloading,operator overloading and template also.

    and second is Run Time polymorphism: by Virtual function

    >>> By the use of virtual function, we can override the function ( b/w base and derived class)

    Code:
    class base
    {
      public:
       void Fun()
      {
        cout<<" I am from base"<<endl;
      }
    }
    
    
    class derived: public Base
    {
       public:
        void fun()
        {
             cout<<" I am from derived"<<endl;
        }
    }
    
    int main()
    {
       Base *bptr;
       Derived dObj;
       bPtr=&dObj;
       bPtr-> fun()
    }
    
    Output will be
    I am from Base
    Because It will never call to derived because No run time polymorphism support here.

    If you write virtual keyword before function then

    Code:
    class base
    {
      public:
       virtual void Fun()
      {
        cout<<" I am from base"<<endl;
      }
    }
    
    
    class derived: public Base
    {
       public:
        void fun()
        {
             cout<<" I am from derived"<<endl;
        }
    }
    
    int main()
    {
       Base *bptr;
       Derived dObj;
       bPtr=&dObj;
       bPtr-> fun()
    }
    
    Output will be
    I am from Derived
    Because It will call to derived because run time polymorphism support because of writing virtual.

    For detail you can see this thread.
    http://www.go4expert.com/showthread.php?t=8403
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice