Virtual function

Use of virtual function in c++

Page 1 of 1

6 Replies - 1015 Views - Last Post: 03 December 2010 - 05:59 AM Rate Topic: -----

#1 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Virtual function

Posted 03 December 2010 - 03:15 AM

Hi,

I confused why virtual function is needed.we can access derived class function by creating derived class object or derived class pointer.Here is my sample code.please help me.

#include <iostream>
using namespace std;

class base 
   {
     public:
         //  virtual void vfunc() {
           void vfunc() {
           cout << "This is base's vfunc().\n";
         }
   };

class derived1 : public base 
  {
      public:
         void vfunc() {
         cout << "This is derived1's vfunc().\n";
       }
  };

class derived2 : public base 
   {
       public:
       void vfunc() {
       cout << "This is derived2's vfunc().\n";
      }
   };
class base1
   {

       public:
       void vfunc() {
       cout << "This is base1's vfunc().\n";

      }

   };

int main()
{

 
  base *p, b;
  derived1 *de1, d1;
  derived2 *de2,d2;


  cout<<"\n\nWith virtual function\n\n\n";
  cout<<"******************************\n";
  p=&b;
  p->vfunc();
  p=&d1;
  p->vfunc();
  p=&d2;
  p->vfunc();
  cout<<"******************************\n";


  cout<<"\n\nWith pointer object\n\n\n";


  cout<<"******************************\n";
  p = &b;
  p->vfunc(); // access base's vfunc()
  de1 = &d1;
  de1->vfunc(); // access derived1's vfunc()
  de2 = &d2;
  de2->vfunc(); // access derived2's vfunc()
  cout<<"******************************\n";
  

  cout<<"\n\nWithout pointer object\n\n\n";
  

  cout<<"******************************\n";
  b.vfunc(); 
  d1.vfunc(); 
  d2.vfunc(); 

 cout<<"******************************\n";



 return 0;
}



Thanks in advance

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 December 2010 - 04:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Virtual function

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Virtual function

Posted 03 December 2010 - 03:28 AM

Compile the following code and see for yourself:
#include <iostream>
#include <vector>
using namespace std;

class base {
public:
    void foo() {
        cout<<"base::foo()"<<endl;
    }   
    virtual void bar() {
        cout<<"base::bar()"<<endl;
    }   
};

class derived: public base {
public:
    void foo() {
        cout<<"derived::foo()"<<endl;
    }   
    void bar() {
        cout<<"derived::bar()"<<endl;
    }   
};

int main() {
    base b;
    derived d;
    vector<base*> vec;
    //we can have pointers to both types
    vec.push_back(&B)/>;
    vec.push_back(&d);
    //show time!
    vec.at(1)->foo(); //what does it print? it's a base pointer to a derived!
    vec.at(1)->bar(); //what does this one print?
    return 0;
}


What does the output say?
Was This Post Helpful? 0
  • +
  • -

#3 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Re: Virtual function

Posted 03 December 2010 - 04:34 AM

Thanks for your valid time Anarion

But my question is why i have to access from base class pointer.virtual functions are required only, if am accessing any functions using base class pointer rite..??.without using base class pointer i can access any functions means what is the use of virtual functions..???


Thanks,
Shimsha
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Virtual function

Posted 03 December 2010 - 04:47 AM

Polymorphism
Was This Post Helpful? 1
  • +
  • -

#5 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Virtual function

Posted 03 December 2010 - 05:02 AM

I wrote a simple tutorial on abstraction that might help. It is by no means extensive, but aimed at introducing the topic with a simple example.

http://www.dreaminco...to-abstraction/
Was This Post Helpful? 1
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Virtual function

Posted 03 December 2010 - 05:07 AM

Why? when you want to make polymorphic objects. Notice how you could use different types of objects(base and derived types) using only one type(pointer to base). Making a function virtual causes dynamic binding to occure, which enables derived types to call their own version of methods, rather than the base method(like what happened to foo and bar).

But you're right, when you know the exact type of the object you will be working with, using a base pointer to access that object does not make any sense. But when you want to make a heterogeneous list/container, you don't know what type you are working with. In this situation, using base pointers seems necessary.

Also, bear in mind that virtual functions have extra overhead because of the virtual function call mechanism; so, you should use it wisely if performance is critical to your program.

This post has been edited by Anarion: 03 December 2010 - 05:10 AM

Was This Post Helpful? 2
  • +
  • -

#7 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Re: Virtual function

Posted 03 December 2010 - 05:59 AM

Thanks everybody... :bigsmile:

Now i got it.Thanks Anarion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1