Virtual functions

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

Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Virtual functions

 
0
  #1
Nov 14th, 2006
I have just started C++, i have problem in understanding virtual functions. can anybody tells me what are v.f. and most important what is there importance in C++.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Virtual functions

 
0
  #2
Nov 14th, 2006
Of my studying and understanding, Virtual Functions are used only for polymorphism. Use virtual functions ONLY if you know those functions wil be overloaded or overrided in derived classes. Don't make a habit of using virtual in any function because "you may need to overload it later on". Now, for a member function to be virtual in a class, both the base class AND derived class must be able to be instantiated. This is very important otherwise the entire concept of virtual functions is lost.
So summing up virtual functions are important to C++ as they are the major factor in polymorphism.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Virtual functions

 
0
  #3
Nov 14th, 2006
>>Now, for a member function to be virtual in a class, both the base class AND derived class must be able to be instantiated

Not really. The base class can have pure virtual functions which means it can not be instantiated on its own. The derived class(s) must override pure virtual functions, while it is optional to override the others. A class can have both virtual and pure virtual functions.

>>Don't make a habit of using virtual in any function because "you may need to overload it later on".

Base classes do not always know if the derived classes need to override the virtual functions, so I see no harm in making virtual functions even though the programmer may not know if they are needed or not. I think the main idea here is: does the base class want to allow derived classes to override the function? If yes, then make it virtual.
Last edited by Ancient Dragon; Nov 14th, 2006 at 8:30 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC