Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   virtual member function.... (http://www.go4expert.com/forums/virtual-member-function-t10314/)

jakeriggs 30Apr2008 22:29

virtual member function....
 
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....

asadullah.ansari 8May2008 19:44

Re: virtual member function....
 
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


All times are GMT +5.5. The time now is 14:53.