virutual function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 24
Reputation: balakrishnan.kb is an unknown quantity at this point 
Solved Threads: 0
balakrishnan.kb balakrishnan.kb is offline Offline
Newbie Poster

virutual function

 
0
  #1
Sep 17th, 2008
hi
what is the purpose of virtual function, please give me idea



thanks
bala
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: virutual function

 
0
  #2
Sep 17th, 2008
you can find loads of examples-definitions on google.. go through them.. then when you have more specific doubts ppl here can help .. happy learning
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: virutual function

 
0
  #3
Sep 17th, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: virutual function

 
1
  #4
Sep 17th, 2008
Virtual functions are the backbone of runtime polymorphism. You can use a member function declared as virtual to redefine the behavior of a base class. Then, when accessing a derived class through a pointer or reference to the base class, the redefined member function will be called:
  1. #include <iostream>
  2.  
  3. class base {
  4. public:
  5. virtual void foo() const { std::cout<<"base\n"; }
  6. };
  7.  
  8. class derived: public base {
  9. public:
  10. virtual void foo() const { std::cout<<"derived\n"; }
  11. };
  12.  
  13. void do_foo ( const base& b )
  14. {
  15. b.foo();
  16. }
  17.  
  18. int main()
  19. {
  20. do_foo ( base() ); // Call 1
  21. do_foo ( derived() ); // Call 2
  22. }
This works because in do_foo, b has two types. The static type is the type of object the code says the reference refers to (base in all cases). The dynamic type is the type of object the reference actually refers to (base in call 1, derived in call 2).

The virtual mechanism is a way to access the dynamic type safely and easily.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC