943,963 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2660
  • C++ RSS
Nov 14th, 2006
0

Virtual functions

Expand Post »
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
Similar Threads
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Nov 14th, 2006
0

Re: Virtual functions

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.
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Nov 14th, 2006
0

Re: Virtual functions

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Using a struct array
Next Thread in C++ Forum Timeline: Font size and layout editing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC